简体   繁体   English

通过复选框和Rails中的下拉菜单更新多个记录。

[英]Update multiple records via checkboxes and dropdown in Rails.

Let's say I have several Posts in an index. 假设我在索引中有多个帖子。

I need to be able to select multiple of them via checkboxes, and then select an action to be done on all selected records (delete, toggle a boolean field in the database to make them all public or private, some other arbitrary method on all of them, etc) using a dropdown with the options and a 'submit' button? 我需要能够通过复选框选择其中的多个,然后选择要对所有选定记录执行的操作(删除,切换数据库中的布尔值字段以使它们全部为公共或私有,对所有等等)使用带有选项和“提交”按钮的下拉菜单?

What is the best and simplest way of achieving this? 实现这一目标的最佳和最简单的方法是什么?

What you can do is to define a method in your controller against your submit action and then loop individually on each item to update each record 您可以做的是针对您的Submit操作在控制器中定义一个方法,然后在每个项目上单独循环以更新每个记录

def update_multiple
  @products = Product.find(params[:product_ids])
  @products.each do |product|
    product.update_attributes!(params[:product].reject { |k,v| v.blank? })
  end
  flash[:notice] = "Updated products!"
  redirect_to products_path
end

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

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