Reputation: 19324
I have an ajax request that sends data to my controller, but i want to add elements to the hash before saving it to the db.
My existing hash looks like this:
{"application_field_attributes"=>{"0"=>{"id"=>"10"}}}
I also have elements params[:xposition]
, and params[:yposition]
that i want to add to the hash above so that it looks like this:
{"application_field_attributes"=>{"0"=>{"id"=>"10", "xposition"=>"1", "yposition"=>"0"}}}
How do i go about doing this?
EDIT
The reason i'm doing this is because i'm trying to keep track of where draggable elements are dropped on a grid.
In my ajax call, i pass in the x and y position which i get from some "data-id" attributes i placed on the grid.
In my controller method, i currently have @application.update_attributes(params[:application])
, but it doesn't contain the xposition and yposition and that's why i'm trying to inject them to the hash.
Maybe a better solution would be to manually update the x and y positions in the controller after the update_attributes command?
So it would look something like:
@application.update_attributes(params[:application])
@application.update_xposition(params[:xposition])
@application.update_yposition(params[:yposition])
Upvotes: 0
Views: 1865
Reputation: 21894
You might want to do that differently.
Maybe a better idea would be to have hidden fields in your form with xposition and yposition, for instance.
Could you tell us more about what you're trying to do, what are x/yposition (if they are different for every application, and where do they come from).
To answer your question though:
app_attrs = {"application_field_attributes"=>{"0"=>{"id"=>"10"}}}
app_attrs["application_field_attributes"]["0"].merge!({
"xposition" => params[:xposition],
"yposition" => params[:yposition]
})
But I wouldn't do that if I were you.
Upvotes: 1