Has_and_belongs_to_many form with check_box

82 views
Skip to first unread message

Greg Ma

unread,
Sep 3, 2010, 2:37:34 PM9/3/10
to rubyonra...@googlegroups.com
Hi,

I have 2 models, user and project.
When I edit my profile I wan't to check the projects I wan't to follow.
So here is my code:


<% form_for @user do |f| %>
...
<% for project in @projects %>
<% fields_for "user[project_attributes][]", project do
|project_form| %>
<li>
<%= project_form.check_box "checked", {:checked =>
@user.project_ids.include?(project.id)} %><label><%= project.name
%></label>
</li>
<% end %>
<% end %>
...
<% end %>

I think the form is correct but the model and controller part is not
very clear...

Here is the log:
"user"=>{"project_attributes"=>{"1"=>{"checked"=>"1"},
"2"=>{"checked"=>"1"}, "3"=>{"checked"=>"0"}, "4"=>{"checked"=>"0"},
"5"=>{"checked"=>"0"}}, "password_confirmation"=>"[FILTERED]",
"password"=>"[FILTERED]"

The project_attributes is really messy and I have to parse the hash to
get selected projects.

There must be a cleaner way??

Greg
--
Posted via http://www.ruby-forum.com/.

Cuz Ican

unread,
Sep 3, 2010, 6:37:14 PM9/3/10
to rubyonra...@googlegroups.com
You seen this?

http://railscasts.com/episodes/17-habtm-checkboxes
http://asciicasts.com/episodes/17-habtm-checkboxes

Seems like you would want to use something like:
@product.categories.include?(category)

except you would be like @user.projects.include?(project)

Reply all
Reply to author
Forward
0 new messages