I'm new to RoR so apologies if the answer is super simple. I'm trying to create a table that allows users to select other users that can collaborate on a wiki. The issue I'm having is that no matter which checkbox you select on the table. It only toggles the topmost option.
here is the code in question:
<%= form_for [@wiki, @wiki.collaborators.build] do |f| %>
<table class="bordered hoverable">
<tbody>
<% @users.each do |user| %>
<tr>
<td><%= user.name %></td>
<td class="right-align"><%= f.check_box :user_id %><%= f.label :user_id, "Give Access" %></td>
</tr>
<% end %>
</tbody>
</table><br /><br />
the controller values in new
def new
@wiki = Wiki.find(params[:wiki_id])
@collaborator = Collaborator.new
@users = (User.all - [current_user])
end
The problem here is that through check_box
's you can't get more than one user selected. In order to select multiple data, you need to use f.collection_select
.
Here's how:
<%= f.collection_select :user_id, @users, :id, :name, {prompt: "Please select collaborators"}, {multiple: true} %>
To select multiple the the name of the checkbox should not be :user
but contain the user id. Try something like that:
<%= form_for [@wiki, @wiki.collaborators.build] do |f| %>
<%= f.fields_for :collaborators do |c| %>
<table class="bordered hoverable">
<tbody>
<% @users.each do |user| %>
<tr>
<td><%= user.name %></td>
<td class="right-align"><%= c.check_box user.id %><%= f.label user.id, "Give Access" %></td>
</tr>
<% end %>
</tbody>
</table><br /><br />
<% end %>
<% end %>
The controller would then recieve params like that:
{:collaborators => {1 => '0', 2 => '1'}
showing that user with id 1 was not checked, user with id 2 was checked
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.