Sencha, Rails3, Forms, and the authenticity token

I’m playing with Sencha Touch right now in a Rails app.  One of the things that hung me up for a bit was the authenticity token that is required with POSTing form data to the rails controller.  Here is how I did it.

In the Sencha file where you declare your FormPanel capture the Rails csrf_token as a variable by adding the following:
var csrf_token = document.getElementsByName("csrf-token")[0].getAttribute("content");
In your FormPanel create a hidden field to submit the token to the controller
items: [
{
xtype: 'hiddenfield',
name: 'authenticity_token',
value: csrf_token
},
... more items ]

Now when you submit the form your authenticity token will be submitted and you can keep the cross-site-scripting forgery protection that Rails provides

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s