加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-16 20:54:57 所属栏目:百科 来源:网络整理
导读:我怎样才能传递数组的值?选中的复选框. 在视图中: = check_box_tag 'user_message_ids[]',user_message.id,false= link_to "buttonBulk Delete/button".html_safe,profile_message_path(user_message),:id = 'user_message_ids',:confirm = "Are you sure?
我怎样才能传递数组的值?选中的复选框.

在视图中:

= 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

我可以将提交按钮放在任何一个区域.

像这个:

= form_tag checked_messages_path do
  = check_box_tag 'user_message_ids[]',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 %>

它会将一组id传递给控制器??,例如{“item_ids []”=> [“1”,“2”,“3”]}

所以你可以用这些ID做任何事情

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

更新(One Small Gotcha)

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

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.

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.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读