简体   繁体   English

在rails上的ruby中选择复选框传递数组

[英]Select checkbox pass array in ruby on rails

How can I pass the value of array? 我怎样才能传递数组的值? of the selected checkbox. 选中的复选框。

In View: 在视图中:

= 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. 我可以将提交按钮放在任何一个区域。

like this one: 像这个:

= form_tag checked_messages_path do
  = check_box_tag 'user_message_ids[]', user_message.id, false
--------objects---------------------------------------------
--------objects---------------------------------------------
--------objects---------------------------------------------
--------objects---------------------------------------------
= submit_tag "Delete Checked"

Use a form_tag block 使用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 %>

It will pass an array of ids to controller, like {"item_ids[]" => ["1", "2", "3"]} 它会将一组id传递给控制器​​,如{“item_ids []”=> [“1”,“2”,“3”]}

So you can do anything with these ids 所以你可以用这些ID做任何事情

FYI: http://railscasts.com/episodes/165-edit-multiple?view=asciicast 仅供参考: http: //railscasts.com/episodes/165-edit-multiple?view = ascicast

Updated (One Small Gotcha) 更新(One Small Gotcha)

From here: http://railscasts.com/episodes/17-habtm-checkboxes?view=asciicast 从这里: http//railscasts.com/episodes/17-habtm-checkboxes?view = ascicast

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 the category_ids are not updated. 如果我们取消选中所有复选框以从所有类别中删除产品,则更新无法删除产品所在的任何类别。这是因为如果取消选中HTML表单上的复选框,则不会将其值发回,因此不会category_ids将出现在产品的参数哈希中,这意味着category_ids不会更新。

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. 要解决此问题, 我们必须修改我们的产品控制器 ,如果未将category_ids传递给更新操作,则将category_ids参数设置为空数组。 We can do this using Ruby's ||= operator and add the following like at the top of the update action. 我们可以使用Ruby's ||=运算符执行此操作,并在更新操作的顶部添加以下内容。

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. 这将确保如果未选中任何复选框,则产品将正确更新,以使其不属于任何类别。

You need to add the button tag around a form tag. 您需要在表单标记周围添加按钮标记。 You cannot submit input tag data using a link_to . 您无法使用link_to提交输入标记数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM