I''ve just had to do something similar and did this (though I have to
say, I''m not wild about it - seems a pretty messy way to do it):
<% for c in @categories %>
<%= check_box("category", c.id, (@selected_categories.include?(c)
?
{:checked => ''checked''} : {:checked => false} )) %>
<%= c.name %>
<% end %>
Basically, this was to allow an edit of an entry which was a member of a
number of categories, with @selected_categories being the categories the
entry is currently a member of. the bit in parentheses tests whether the
category is included in @selected_categories, if so it returns :checked
=> ''checked'', otherwise {:checked => false}
However, this feels a bit like a hack to me, and there''s got to be a
better way. Hope this helps in the meantime.
Chuck Vose wrote:> It''s a little obscure but you can find some general suggestions
here:
>
http://api.rubyonrails.com/classes/ActionView/Helpers/FormHelper.html#M000394
>
>
> I remember having a hell of a time doing the same thing but I
don''t
> remember my solution.
>
> On 5/1/06, Nick Coyne <nic@nickcoyne.com> wrote:
>> I have a series of checkboxes in a form generated as follows:
>>
>> <% for user in @active_users %>
>> <p><%= user.firstname + " " + user.surname%>
>> <%= check_box ''assigned_tasks'',
''user_id'', {:index => user.id},
>> user.id, nil %></p>
>> <% end %>
>>
>> I''d like to have one of them selected when user.id is equal to
the value
>> of the logged in user, stored in session[:user_id]
>>
>> How do I do that?
>>
>> thx
>> N.
>>
>> --
>> Posted via http://www.ruby-forum.com/.
>> _______________________________________________
>> Rails mailing list
>> Rails@lists.rubyonrails.org
>> http://lists.rubyonrails.org/mailman/listinfo/rails
>>
> _______________________________________________
> Rails mailing list
> Rails@lists.rubyonrails.org
> http://lists.rubyonrails.org/mailman/listinfo/rails
>