ExtJS with Rails 3
Filed in: extjs, integration, Rails Add comments
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 |
March 8th, 2011 at 20:22
Could you be more specific on where to put these tweaks within the rails project?
Thnks in advance.
Reply
Roman Reply:
March 9th, 2011 at 19:34
You put the javascript snippets in your javascript files, where you think it fits. The rails part goes to configuration.
Reply
March 25th, 2011 at 06:50
Thank you so much for this, have been wondering how to make EXTJS work with the XSRF protection.
Reply