In my Vue frontend I have set this method:
bulkAction(selected) {
this.$secured.patch('/api/v1/bulk_edit', {
data: this.selected,
sold: this.bulk_sold
})
.then((response) => {
console.log(response.data)
});
}
selected: [] is an array of objects.
sold is a boolean and an attribute in the objects in the array selected.
So for instance if sold is true, I want to set sold as true for all objects in the array.
With this in place I am able to send to the server an array of objects(data) and a param sold.
In rails controller I have set this methd:
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
Which returns:
NoMethodError (undefined method `update' for #<ActionController::Parameters:0x00007fa094596140>):
I guess this is the wrong part:
@item_locations = ItemLocation.where(params[:data])
But I'm not sure on how to convert it.
The request looks like:
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"=>{}}
Any suggestion?
updatewhat you receive from the request, which is converted into anActionController::Parametersobject.params[:data]) in order to query the records from the DB?