PDA

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



kpalser
24 May 2010, 8:42 AM
With Ext Scaffold Generator Plugin Rails is returning the following JSON structure:
{"pacientes":[{"paciente":{"paciente_id":1,"nombre":"Kate","apellidos":"Luis"}}],"results":1}

I think it should be:
{"pacientes":[{"paciente_id":1,"nombre":"Kate","apellidos":"Luis"}],"results":1}

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,
});

storePacientes.load();

console.log(storePacientes);
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?

kpalser
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]}
end
end