I find the documentation for collection check boxes to be hard to understand. The explanation for the parameters is:
collection_check_boxes(object, method, collection, value_method, text_method, options = {}, html_options = {}, &block) public
Returns check box tags for the collection of existing return values of method for object's class. The value returned from calling method on the instance object will be selected. If calling method returns nil, no selection is made.
What I'm not sure about is what the "object" (the first argument) is supposed to be. I tried two different possibilities, and got different failures.
<%= collection_check_boxes(:wiki, :collaborating_user_ids, User.all, :id, :name) do |b| %>
<li>
<%= b.check_box %>
<%= b.label %>
</li>
<% end %>
The idea is that my app supports editing of wikis. I want the wiki owner to be able to add and remove users from the list of collaborators using checkboxes (which of course is only practical when there is a small number of users, which is the case). collaborating_user_ids
is an instance method of Wiki
that returns the current list of collaborators. I want to display the names of all users, with the current collaborators checked.
This works for the display, but when I inspect the params
returned by this form, there is nothing showing which checkboxes were checked. On the other hand, if I replace the symbol :wiki
by the instance variable @wiki
, the checkboxes appear in the params
associated with the wiki, but the checkboxes are not set correctly, initially.
I don't actually know what the first parameter of collection_check_boxes
is supposed to be, but every example I've seen shows a symbol.
Aucun commentaire:
Enregistrer un commentaire