In my database I have two tables that has a many-to-many relationship, and by that reason I’ve created a relationship table.
Projects
Users
Project_users <-- members of a project
I’m working on the view for editing a project, which contain of a form with all the project fields, and for the members of a project I render out checkboxes. This works, but with the code below all the checkboxes is checked, even for the users that’s not members of the project.
So, how should I change the code so only the checkboxes for the current members of the project gets checked?
edit project view:
<%= form_for @project do |f| %>
...
the rest of the form
...
<div class="checkbox">
<% @members.each do |user| %>
<%= check_box_tag "project[members][]", user.id, '1', :id => "user_#{user.id}" %>
<%= label_tag "user_#{user.id}", user.first_name + ' ' + user.last_name, :class => "checkbox" %>
<% end %>
</div>
...
the rest of the form
...
<% end %>
projects controller:
...
def edit
@project = Project.find(params[:id])
@members = @project.users
end
...
The third argument to the
check_box_tagis the boolean for the check(ed) state:See the doc:
check_box_tag(name, value = "1", checked = false, options = {})In your case: