ExtJS with Rails 3

In order to make ExtJS play nicely with Rails, the following tweaks are needed:

On the ExtJS javascript side, the following things are needed:

1. Ask server to server JSON:

Ext.Ajax.defaultHeaders = {'Accept': 'application/json'};

2. RailsJsonStore:

Ext.data.RailsJsonStore = Ext.extend(Ext.data.JsonStore, {
    constructor: function(config) {
        Ext.data.RailsJsonStore.superclass.constructor.call(this, Ext.applyIf(config, {
            messageProperty: 'message', //for store.reader.getMessage()
            restful: true,
            url: '/'+ config['root'] + 's',
            writer: {encode: false}
Ext.reg('railsjsonstore', Ext.data.RailsJsonStore);

3. XSRF protection:

Ext.Ajax.on('beforerequest', function(o) {
        var csrf = Ext.select("meta[name='csrf-token']").first();
        if (csrf) {
                o.defaultHeaders = Ext.apply(o.defaultHeaders || {}, {'X-CSRF-Token': csrf.getAttribute('content')});

On Rails side:

config.active_record.include_root_in_json = false

3 Responses to “ExtJS with Rails 3”

  1. Victor Says:

    Could you be more specific on where to put these tweaks within the rails project?

    Thnks in advance.


    Roman Reply:

    You put the javascript snippets in your javascript files, where you think it fits. The rails part goes to configuration.


  2. Daniel Nolan Says:

    Thank you so much for this, have been wondering how to make EXTJS work with the XSRF protection.


Leave a Reply