Skip to content

Instantly share code, notes, and snippets.

@TechFounder
Created August 7, 2018 14:48
Show Gist options
  • Save TechFounder/00a37ad43538da5e95798f10a4ed611d to your computer and use it in GitHub Desktop.
Save TechFounder/00a37ad43538da5e95798f10a4ed611d to your computer and use it in GitHub Desktop.
Two method of using checkboxes on forms with Bootstrap 4
# Store#performance
# This is the old way of doing checkbox collections.
# Note the hidden field tag to make sure that unchecked items are passed to the server
<fieldset class='form-group mx-sm-3'>
<legend class='pt-4'>Performance</legend>
<div class='row'>
<% hidden_field_tag 'store[performance_ids][]', nil %>
<% Performance.all.each do |performance| %>
<div class='col col-sm-6 pt-2'>
<label class="active">
<%= check_box_tag 'store[performance_ids][]',
performance.id,
@store.performance_ids.include?(performance.id),
id: dom_id(performance),
autocomplete: false %>
<%= performance.name %>
</label>
</div>
<% end %>
</div>
</fieldset>
# Store#offering
# This new collection_check_boxes helper was created in Rails 4
# It's much cleaner
<fieldset class='form-group mx-sm-3'>
<legend class='pt-4'>Offerings</legend>
<div class='row'>
<%= collection_check_boxes(:store, :offering_ids, Offering.all, :id, :name) do |b| %>
<div class='col col-sm-6 pt-2'>
<%= b.check_box %> <%= b.label %>
</div>
<% end %>
</div>
</fieldset>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment