The following array of boolean attributes for multiple records
{"utf8"=>"✓","_method"=>"patch", "authenticity_token"=>"...",
"ts"=>
{"1"=>{"go"=>"0", "pickup"=>"0", "delivery"=>"1"},
"2"=>{"go"=>"0", "pickup"=>"0", "delivery"=>"1"},
"3"=>{"go"=>"0", "pickup"=>"0", "delivery"=>"1"},
[...]},
"commit"=>"Save changes"}
is being posted from one controller to a child controller with the following action that has un-conventional naming for the parameters.
def update_all
params[:ts].keys.each do |id|
@daystruttimeslot = Daystruttimeslot.find(id.to_i)
@daystruttimeslot.update(ts_params)
end
end
is hitting the error undefined local variable or method 'ts_params' for #<DaystruttimeslotsController:0x00007fa118f262f8> Did you mean? to_param params @_params
undefined local variable or method 'ts_params' for #<DaystruttimeslotsController:0x00007fa118f262f8> Did you mean? to_param params @_params
How can these parameters be properly processed by this action?
def update_all
ts = params.require(:ts)
@daystruttimeslots = Daystruttimeslot.where(id: ts.keys)
@daystruttimeslots.each do |d|
d.update(ts.fetch(d.id.to_s).permit(:go, :pickup, :delivery))
end
end
This does a single read operation instead of fetching each record separately and also provides a ivar that actually makes sense instead of whatever is at the end of the loop.
If you need to validate that all the ids are correct compare ts.keys.length
to @daystruttimeslots.size
. You also might want to consider wrapping this in a transaction so that the changes are rolled back if any of the updates fail instead of just leaving the job half done.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.