MarkUs Blog

MarkUs Developers Blog About Their Project

Problems getting Prototype/Ajax.Request to talk to Rails

with 2 comments

I’m totally losing my mind over this problem, because it seems so simple, and yet I can’t seem to figure it out.

Here’s the situation:

I’ve begun linking up my Rubric Manager UI prototype to the Rails backend.  On this UI is a big “Save Changes” button, that implements all of the changes to the Rubric in one shot.  That button is linked to a Javascript function called save_changes.

Here is save_changes:

function save_changes() {
/* ....some code...*/
  new Ajax.Request('/checkmark/rubrics/modify',
  {
      method:'post',
      parameters: {authenticity_token: '<%=form_authenticity_token%>', key: 'value'},
      onSuccess: function(transport){
      console.log("Success");
  },
  onFailure: function(){ console.error('Something went wrong...') }
  });
/*...some more code...*/
}

So this seems alright to me.  I have to include the “authenticity_token” in my parameters because Rails seems to be using some type of internal request forgery protection.

When I click on “Save Changes”, however, Firebug tells me that the page I POSTED to threw an “InvalidAuthenticityToken” exception.  So that’s frustrating – I’m clearly passing the token in my parameters.  I created a tiny test form and POSTed to the same ‘modify’ method with the authenticity token as a hidden value, and it worked just fine.

So what gives?

Geofrey helped me get by the InvalidAuthenticityToken problem by inserting the authenticity_token variable into the URL string of the Ajax.Request target:

function save_changes() {
/* ....some code...*/
  new Ajax.Request('/checkmark/rubrics/modify?authenticity_token=43e5f5e3fbf3248b2a403f828010f8544ad36dbf',
  {
      method:'post',
      parameters: {key: 'value'},
      onSuccess: function(transport){
      console.log("Success");
  },
  onFailure: function(){ console.error('Something went wrong...') }
  });
/*...some more code...*/
}

But NOW, the parameter “key => value” isn’t being passed to the rails param map in the RubricsController.  I know this, because I inspect the param map in the Controller with the following code:

def modify
    render :text => params.inspect  
end

And here’s what is returned from the AJAX call:

{"authenticity_token"=>"43e5f5e3fbf3248b2a403f828010f8544ad36dbf", "action"=>"modify", "controller"=>"rubrics"}

So what’s going on?  Anyone.

-Mike

Written by m_conley

November 14th, 2008 at 12:41 am

Posted in Uncategorized

Tagged with , , , , ,

2 Responses to 'Problems getting Prototype/Ajax.Request to talk to Rails'

Subscribe to comments with RSS or TrackBack to 'Problems getting Prototype/Ajax.Request to talk to Rails'.

  1. I would get an uncompressed version of Prototype into the dev environment, read the Ajax.Request code, and set some breakpoints with Firebug.

    But is the problem on the frontend or the backend? Check the xhr object in Firebug to see if the parameters are getting passed in the HTTP request.

    Jeff Balogh

    16 Nov 08 at 12:46 pm

  2. Hey – thanks for responding!

    The HTTP request going out looks fine – the parameters are being sent by POST without a problem.

    I did a little detective work though – I changed the modify method to this:

    def modify
    render :text => request.inspect
    end

    It dumped the request object, and I can see my parameters in there as instance variables:

    @parameters={“action”=>”modify”, “controller”=>”rubrics”, “key”=>”value”},

    But for some reason, these aren’t getting to the params hash. I also can’t find any accessor methods for those parameters – I believe Rails expects me to get them from params. So I suspect something is going wrong in the Rails backend.

    Any ideas for where to dig around?

    Thanks,

    -Mike

    Mike

    16 Nov 08 at 1:21 pm

Leave a Reply