Skip to content

Instantly share code, notes, and snippets.

@gathuku
Last active February 23, 2021 12:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gathuku/438aa16edb687ddef92d2c67d8d1368a to your computer and use it in GitHub Desktop.
Save gathuku/438aa16edb687ddef92d2c67d8d1368a to your computer and use it in GitHub Desktop.
Rails checkbox form
<%= form_with(model: user_group, local: true) do |form| %>
<% if user_group.errors.any? %>
<div id="error_explanation" class="alert alert-danger">
<h5><%= pluralize(user_group.errors.count, "error") %> prohibited this user group from being saved:</h5>
<ul>
<% user_group.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="">
<%= form.hidden_field :account_id, value: current_user&.account&.id %>
</div>
<div class="form-group">
<%= form.label :name %>
<%= form.text_field :name, class: 'form-control' %>
</div>
<div class="form-group">
<%= form.label :desc %>
<%= form.text_field :desc, class: 'form-control' %>
</div>
<div class="form-group">
<table class="table">
<thead>
<th>Resource name</th>
<th>Actions</th>
</thead>
<tbody>
<%= form.fields_for :permissions do |form| %>
<%= render "permission_fields", form: form %>
<% end %>
</tbody>
</table>
</div>
<div class="form-group">
<% if user_group.persisted? %>
<div class="float-right">
<%= link_to 'Destroy', user_group, method: :delete, class: "text-danger", data: { confirm: 'Are you sure?' } %>
</div>
<% end %>
<%= form.submit class: 'btn btn-primary' %>
<% if user_group.persisted? %>
<%= link_to "Cancel", user_group, class: "btn btn-link" %>
<% else %>
<%= link_to "Cancel", user_groups_path, class: "btn btn-link" %>
<% end %>
</div>
<% end %>
<tr>
<td> <%= form.text_field :controller_name %> </td>
<td>
<%= form.fields_for :actions do |field|%>
index<%= field.check_box :index %>
show<%= field.check_box :show %>
<% end %>
</td>
</tr>
class UserGroup < ApplicationRecord
belongs_to :account
has_many :permissions, dependent: :destroy
validates :desc, presence: true
validates :name, presence: true, uniqueness: true
accepts_nested_attributes_for :permissions, allow_destroy: true
end
@gathuku
Copy link
Author

gathuku commented Feb 23, 2021

Screen Shot 2021-02-23 at 3 34 15 PM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment