Skip to content

Instantly share code, notes, and snippets.

@jiikko
Forked from jabbett/_form.html.erb
Last active July 22, 2017 01:13
Show Gist options
  • Save jiikko/e012dd897a526ae80bc5486924e19459 to your computer and use it in GitHub Desktop.
Save jiikko/e012dd897a526ae80bc5486924e19459 to your computer and use it in GitHub Desktop.
ActsAsTaggableOn with collection_check_boxes
<%= form_for(@sundae) do |f| %>
<!--
collection_check_boxes requires 4 parameters, the last two are methods that access
the value and text from the collection, respectively. Hence the need for
SundaesHelper.valid_flavors!
-->
<%= f.collection_check_boxes(:flavor_list, Sundae::FLAVORS, :itself, :itself) do |b| %>
<!-- FYI: I use Bootstrap 4, so I customized how the checkboxes would render -->
<div class="form-check">
<label class="form-check-label"><%= b.check_box class: 'form-check-input' %> <%= b.value %></label>
</div>
<% end %>
<%= f.submit %>
<% end %>
class Sundae < ApplicationRecord
FLAVORS = ['Chocolate', 'Vanilla', 'Strawberry', 'Grapenut']
acts_as_taggable_on :flavors
end
class SundaesController < ApplicationController
# ...
private
def sundae_params
params.require(:outreach).permit(:flavor_list => [])
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment