mardi 6 septembre 2016

How to mark the simple_form checkbox based on a Array object?

The Rails view has two parameters

list_of_attributes = ["a1", "a2", "a3", "a4"]
user_selections = ["a2", "a4"]

I am able to display the appropriate checkboxes and any associated user selections using the following simple_form definition

<% list_of_attributes.each do |item| %>
   <label>
     <%= check_box_tag "user_selections[]", item, user_selections.include?(item) %>
     <%= item %>
   </label>
<% end %>

How can I define the above behavior using simple_form f.input syntax? With the following definition, I am able to display the appropriate checkboxes, but any user selections is not 'checked'.

<%= f.input key, as: :check_boxes, collection: list_of_attributes, 
 :label => key, class: "form-control" %>

Thanks for your help.




Aucun commentaire:

Enregistrer un commentaire