How to read JSON from Ruby on Rails with ExtJS
extjs, javascript, json, ruby, ruby-on-rails
Solution
basically :
ActiveRecord::Base.include_root_in_json = false
or
YourClass.include_root_in_json = false
as described here: http://apidock.com/rails/ActiveRecord/Serialization/to_json
Problem
I have some existing projects that were built upon a deprecated PHP framework, and I'm hoping to move them over to Ruby on Rails with minimal effort. My main problem right now is the format that the JSON is coming back in. My frontend code (all ExtJS) is expecting JSON in the format: ``` { "result": [ [id: 1, name: "mike"], [id: 2, name: "john"], [id: 3, name: "gary"] ] } ``` But the default return from Ruby on Rails is as follows: ``` { "result": [ {"record" : {id: 1, name: "mike"}}, {"record" : {id: 2, name: "john"}}, {"record" : {id: 3, name: "gary"}} ] } ``` My controller is basically doing nothing but: ``` @records = Record.find(:all) respond_to do |format| format.json { render :text => @records.to_json} end ``` As you can see, it's adding in an additional key to every record, which my frontend ExtJS code is not capable of parsing as-is. Is there any way to stop this from occuring? Thanks for any help you can offer, Mike Trpcic