Reputation: 87
I have a list of FAQs, and I want each to have a checkbox to select/de-select and then submit the selected ones as an array.
In my view I have:
<%= f.input :faqs, collection: @faqs, as: :checkboxes %>
Which gives me a list of all my FAQs in the collection with checkboxes. What I'm trying to do is list the FAQs grouped by category. Each category will also have a checkbox that if checked will check all of the FAQs under that category. My plan was that the category checkboxes will be used for JS to check all of the FAQs in that category, but won't be passed as a param when submitting the form.
In my controller I've grouped my faqs:
@grouped_faqs = @faqs.group_by { |faq| FaqCategory.find(faq[:faq_category_id]).name }
Which I can then use to list my FAQs by category in my view, but outside of the form (so purely visual).
If I use @grouped_faqs in place of @faqs as the collection for the input then I get a list of categories checkboxes, rather than the individual FAQs.
Is there something like grouped_select that can be used for checkboxes?
Upvotes: 2
Views: 435
Reputation: 87
I think I've solved this.
So in my contoller I have the faqs grouped by category:
@grouped_faqs = @faqs.group_by { |faq| FaqCategory.find(faq[:faq_category_id]).name }
And in my view:
<% @grouped_faqs.each do |cat, faqs| %>
<%= f.input :faqs, as: :check_boxes, collection: faqs, label: cat %>
<% end %>
This gives me a list of category names with the corresponding FAQs listed as checkboxes under each category.
Upvotes: 1