[英]checkboxs in a table created via form_for
I'm new to RoR so apologies if the answer is super simple. 我是RoR的新手,所以如果答案很简单,我深表歉意。 I'm trying to create a table that allows users to select other users that can collaborate on a wiki.
我正在尝试创建一个表,该表允许用户选择可以在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. 这里的问题是,通过
check_box
不能选择多个用户。 In order to select multiple data, you need to use f.collection_select
. 为了选择多个数据,您需要使用
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. 要选择多个,复选框的名称不应为
: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 显示未检查ID为1的用户,检查了ID为2的用户
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.