1

I'm new to rails, can somebody help me with json format that rails 3.2.2 produce I want to know where and how i need to make changes to affect my json otput. For example when i click http://localhost:3000/customers.json i got output like this

[{"created_at":"2012-03-17T16:10:59Z","id":1,"name":"Jon","phone":"59665","updated_at":"2012-03-17T16:10:59Z"}]

, but i need ==>

[{"customer":{{"created_at":"2012-03-17T16:10:59Z","id":1,"name":"Jon","phone":"59665","updated_at":"2012-03-17T16:10:59Z"}}]

Thanks in advance.

1

1 Answer 1

4

You'll want to set ActiveRecord::Base.include_root_in_json = true

Try setting that in an initializer (config/initializers), restarting your server. You should see the root object appear in your json.

Sign up to request clarification or add additional context in comments.

2 Comments

Can anyone think of a way to override the use of the model class name as the root element (i.e. set include root to true but specify a different name). I have an external app using the JSON and would rather not have to change my model class name (or change the JSON after the fact or any similar ugliness).
Answering my own question...apparently as simple as myobj.to_json(:root => "rootname")

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.