4

I am using pretty_generate in my controller, but I am getting the following error

'only generation of JSON objects or arrays allowed'

@celebrities = Celebrity.includes(:category)    
respond_to do |format|
  format.json { render json: JSON.pretty_generate(@celebrities.to_json(:include =>{:category => {:only => [:category]} })) }
end

I am not sure why I am getting this error

4
  • Step through it in the console, eg do @celebrities.to_json(:include =>{:category => {:only => [:category]} }), store the result in a variable, call JSON.pretty_generate on it etc. Commented Sep 7, 2015 at 13:23
  • @MaxWilliams Same error, do I need to add require json somewhere Commented Sep 7, 2015 at 13:30
  • Possibly, try require 'json' Commented Sep 7, 2015 at 13:55
  • possible duplicate of How can I "Pretty" format my JSON output in Ruby on Rails? Commented Sep 7, 2015 at 14:04

1 Answer 1

3

As the error suggest, only generation of JSON objects or arrays allowed. I guess you should try this.

@celebrities = Celebrity.includes(:category)    
respond_to do |format|
  format.json { render json: JSON.pretty_generate(JSON.parse(@celebrities.to_json(:include =>{:category => {:only => [:category]} })))}
end
Sign up to request clarification or add additional context in comments.

Comments

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.