View Full Version : Problem with JOSN structure returned from Rails?

24 May 2010, 8:42 AM
With Ext Scaffold Generator Plugin Rails is returning the following JSON structure:

I think it should be:

My code:

var proxyPacientes = new Ext.data.ScriptTagProxy({ // cross domain
//var proxyPacientes = new Ext.data.HttpProxy({
url : dataUrl + 'pacientes.ext_json',

var storePacientes = new Ext.data.JsonStore({
totalProperty : 'results',
fields : fields,
proxy : proxyPacientes,
root : 'pacientes',

//autoLoad : true,


Using Firebug I can see that storePacientes.data.items remains empty. Although the terminal running Rails shows that SELECT * FROM `pacientes` is being processed no records are being loaded.

Is the JSON structure wrong? Any ideas where/what I should be changing in Rails or Ext JS?

25 May 2010, 2:58 AM
I solved with the help of Martin Rehfeld <[email protected]>! There were two issues:

1) In Rails my ./config/initializers/new_rails_defaults.rb had Base.include_root_in_json set to true. It needs to be false to format the JSON structure as specified in my previous post.

2) ScriptTagProxy expects JSON to be wrapped in a callback function that is sent as a parameter to Rails:
e.g. http://localhost/pacientes.ext_json?_dc=1274783988090&callback=stcCallback1001

Hence the controller in Rails needs ":callback => params[:callback]":

# GET /pacientes
# GET /pacientes.ext_json
def index
respond_to do |format|
format.html # index.html.erb (no data required)
format.ext_json { render :json => @pacientes.to_ext_json(:class => Paciente, :count => Paciente.count(options_from_search(Paciente))), :callback => params[:callback]}