Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vue.js/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails Vue axios将数组作为参数传递到Rails并全部更新(批量编辑)_Ruby On Rails_Vue.js - Fatal编程技术网

Ruby on rails Vue axios将数组作为参数传递到Rails并全部更新(批量编辑)

Ruby on rails Vue axios将数组作为参数传递到Rails并全部更新(批量编辑),ruby-on-rails,vue.js,Ruby On Rails,Vue.js,在我的Vue前端中,我设置了以下方法: bulkAction(selected) { this.$secured.patch('/api/v1/bulk_edit', { data: this.selected, sold: this.bulk_sold }) .then((response) => { console.log(response.data) }); } selected:[]是一个对象数组。 sall是一个布尔值,是选定

在我的Vue前端中,我设置了以下方法:

bulkAction(selected) {
  this.$secured.patch('/api/v1/bulk_edit', {
     data: this.selected,
     sold: this.bulk_sold
  })
    .then((response) => {
    console.log(response.data)
});
    }
selected:[]
是一个对象数组。
sall
是一个布尔值,是选定数组中对象的一个属性

例如,如果sell为true,我想为数组中的所有对象设置sell为true

有了它,我就能够向服务器发送一组对象(数据)和一个参数

在rails控制器中,我设置了以下方法:

def bulk_edit
    @item_locations = ItemLocation.where(params[:data])
    bulk_params = params.permit(:sold)
    @item_locations.each do |item_location|
      item_location.update(bulk_params)
    end
end
返回:

NoMethodError (undefined method `update' for #<ActionController::Parameters:0x00007fa094596140>):
但我不知道如何转换它

请求如下所示:

    app/controllers/api/v1/items_controller.rb:47:in `bulk_edit'
Started PATCH "/api/v1/bulk_edit" for 127.0.0.1 at 2020-01-20 16:40:28 +0800
Processing by Api::V1::ItemsController#bulk_edit as HTML
  Parameters: {"data"=>[{"id"=>"44ed3183-cce2-4f3b-b673-6052d8cc5fe5"}, {"id"=>"ed7438dc-60ed-4887-bf86-90b6161c0074"}], "sold"=>false, "item"=>{}}
有什么建议吗?

试图简化它

def bulk_edit
    @item_locations = ItemLocation.where(id: params[:data]).update_all(params.permit[:sold])
end

根据您发送的内容,您需要首先映射
数据
中的内容,以获取每个ItemLocation id,这样您就可以在查询中进行

ItemLocation
  .where(id: params['data'].map { |item_location| item_location['id'] })
  .update_all(sold: params['sold'])

在此之后,您可以使用
update\u all
执行单个查询来更新您获得的每个ItemLocation,sall的值等于参数中的值,

您必须首先在调用更新中将这些记录作为ActiveRecord对象获取。否则,您只需尝试更新从请求收到的内容,该内容将转换为
ActionController::Parameters
对象。您要发送的标识符是什么(
params[:data]
)为了从数据库中查询记录?你能在回答中演示如何转换它吗?@SebastianPalma它是IDB,但如果我不能转换对象,我必须迭代转换到活动记录,在那一点上,我可以只传递ID而不是对象。谢谢回答,但这不会返回要更新的记录。你需要映射进来的内容
参数[:数据]
where
不会自动从中检索内容。TS说他已经更新了请求正文,现在只发送ID数组。所以我想我们可以在他们身上做些什么。或者我误解了什么
ItemLocation
  .where(id: params['data'].map { |item_location| item_location['id'] })
  .update_all(sold: params['sold'])