Using prototype to mimic html form
I have an html form generated by rails, so that when it's submitted, the rails controller can gather all the params for the rails object in one swoop with:
@car_object = Car.new(params[:car])
which results in something like:
@car_object.color = "red";
@car_object.make = "Honda"
etc...
But I also need to have the controller be able to extract the same params when submitted via prototype. I'm currently doing it like:
req = new Ajax.Request('/car', {
method: 'post',
parameters: { authenticity_token: getAuthKey(),
color: color,
make: make
},
In which case the controller has to manually create the car object by getting the individual开发者_运维技巧 parameters and assigning them to the new Car object.
How would I create this Ajax request so that when the Rails controller gets it, params[:car] can extract the parameters and assign them to the new Car object on it's pwn?
I haven't used Prototype in a while, so forgive the quick guess, but have you tried submitting this way instead?
req = new Ajax.Request('/car', {
method: 'post',
parameters: {
authenticity_token: getAuthKey(),
car: {
color: color,
make: make
},
It turns out I wasn't that far off, but in my case I needed to include the keeper_id in the following format:
parameters: { authenticity_token: getAuthKey(),
color: color,
make: make,
'dealership[keeper_id]': dealership_id
},
Thanks guys!
I'm not too sure on the exact specifics of your requirements as i'm not familiar with Rails. According to the docs Its possible with Prototype though to pass an object or a hash as your parameters in Ajax.Request
精彩评论