views:

31

answers:

3

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?

+1  A: 

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
        },
adriandz
A: 

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

seengee
A: 

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!

99miles