1

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)

1 Answer 1

2
<% @users.each do |user| %>
    <%= check_box_tag "project[members][]", user.id, checked_or_not, :id => "user_#{user.id}" %> 
    <%= label_tag "user_#{user.id}", user.name_login_or_smth_else %>
<% end %>

Replace checked_or_not with code that indicates that user.id is already in members array. For example if you setup has_and_belongs_to_many relation between projects and users like this:

class Project < ActiveRecord::Base
    has_and_belongs_to_many :members, :class_name => 'User'
end

then form would be:

<% @users.each do |user| %>
    <%= check_box_tag "project[member_ids][]", user.id, @project.member_ids.include?(user.id), :id => "user_#{user.id}" %> 
    <%= label_tag "user_#{user.id}", user.name_login_or_smth_else %>
<% end %>

UPDATE:

Second argument of check_box_tag user.id is the value of checkbox, third @project.member_ids.include?(user.id) is checkbox checked or not. You can read more about it here.

has_and_belongs_to_many adds collection_ids method that returns array of all associated objects’ ids. Since we want to name association members we are setting up association with class_name option:

has_and_belongs_to_many :members, :class_name => 'User'

and collection_ids method becomes member_ids. So @project.member_ids.include?(user.id) sets checkbox checked if members already include user and not checked otherwise.

From another your question I can assume that you setup association like this:

has_and_belongs_to_many :users

Then it will add user_ids method and in form you should use

check_box_tag "project[user_ids][]", user.id, @project.user_ids.include?(user.id)

You can read here for details.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks a lot for the answer. However, when trying out the code I get the following error: undefined method `member_ids' for... Do you know why? Could you maybe explain "user.id, @project.member_ids.include?(user.id)" for me?
Have you setup has_and_belongs_to_many association and checked that is correct and working? I updated my answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.