I want to dynamically create checkboxes in a form with the use of an array with users. The value that should be saved to the array (param[:project][:members]) should contain of the user_id's from all the clicked check boxes with users.
I have been looking around a lot but haven't yet found what I look for.
What code should I write for the checkboxes?
Form in view:
<%= form_for @project do |f| %>
<div class="text_field">
<%= f.label :title%>
<%= f.text_field :title%>
</div>
<div class="text_field">
<%= f.label :description%>
<%= f.text_field :description%>
</div>
<div class="dropdown">
<%= f.label :start_date%>
<%= f.date_select :start_date %>
</div>
<div class="dropdown">
<%= f.label :end_date%>
<%= f.date_select :end_date %>
</div>
<div class="checkboxes">
<% @users.each do |user| %>
// <%= check_box_tag "users[]", user.id %> <--- ???
<% end %>
</div>
<div class="submit">
<%= f.submit "Spara" %>
</div>
<% end %>
controller:
def new
@project = Project.new
@users = (current_user.blank? ? User.all : User.find(:all, :conditions => ["id != ?", current_user.id]))
end
rendered html:
[ ] Jules Smith // user_id: 1
[X] Carl Jones // user_id: 2
[X] Lily Stevens // user_id: 3
// param[:project][:members] // <-- 2, 3 (user_id's)