在rails上的ruby中选择复选框pass array [英] Select checkbox pass array in ruby on rails
问题描述
如何传递array的值?的所选复选框。
How can I pass the value of array? of the selected checkbox.
在视图中:
= check_box_tag 'user_message_ids[]', user_message.id, false
= link_to "<button>Bulk Delete</button>".html_safe, profile_message_path(user_message), :id => 'user_message_ids', :confirm => "Are you sure?", :method => :delete
,我可以将提交按钮放在这个区域的任何一个区域。
and can I place the submit button in any of this area.
像这样:
= form_tag checked_messages_path do
= check_box_tag'user_message_ids []',user_message.id,false b $ b --------- objects ------------------------------------- --------
--------- objects ---------------------------- -----------------
--------- objects ------------------- --------------------------
--------- objects ---------- -----------------------------------
= submit_tag删除已选中
= form_tag checked_messages_path do = check_box_tag 'user_message_ids[]', user_message.id, false ---------objects--------------------------------------------- ---------objects--------------------------------------------- ---------objects--------------------------------------------- ---------objects--------------------------------------------- = submit_tag "Delete Checked"
推荐答案
使用 form_tag
阻止
<% form_tag delete_mutiple_items_path do %>
<table>
<thead>
<tr>
<th></th>
</tr>
</thead>
<tbody>
<% @items.each do |item| %>
<tr>
<td><%= check_box_tag "items[]", item.id %></td>
</tr>
<% end %>
</tbody>
</table>
<%= submit_tag "delete Checked" %>
<% end %>
它会传递一个ids数组到控制器,如{item_ids []=> 1,2,3]}
It will pass an array of ids to controller, like {"item_ids[]" => ["1", "2", "3"]}
因此,您可以对这些ids做任何事。
So you can do anything with these ids
FYI: http://railscasts.com/episodes/165-edit-multiple?view=asciicast
从这里开始: http://railscasts.com/episodes/17-habtm-checkboxes?view=asciicast
我们的update方法还有一个小问题。 如果我们取消选中所有复选框以从所有类别中移除某个产品,则更新将失败,以删除该产品所属的任何类别。这是因为HTML表单上的复选框不会将其值发回如果未选中,因此在产品的参数哈希中不会出现
category_ids
,这意味着category_ids
未更新。
There is still one small problem with our update method. If we uncheck all of the checkboxes to remove a product from all categories then the update fails to remove any categories the product was in. This is because a checkbox on an HTML form will not have its value sent back if it is unchecked and therefore no
category_ids
will appear in the product’s parameters hash, meaning that thecategory_ids
are not updated.
要解决此问题我们必须修改我们的产品控制器,以设置 category_ids
参数如果未传递到更新操作,则为空数组。我们可以使用 Ruby's || =
运算符,并在更新操作的顶部添加以下内容。
To fix this we have to modify our products controller to set the category_ids
parameter to be an empty array if it is not passed to the update action. We can do this using Ruby's ||=
operator and add the following like at the top of the update action.
params [:product] [:category_ids] || = []
选中复选框,然后正确更新产品,使其不属于任何类别。
This will ensure that if none of the checkboxes are checked then the product is updated correctly so that it is in no categories.
这篇关于在rails上的ruby中选择复选框pass array的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!