saravana
saravana

Reputation: 331

Pretty format my JSON output in Rails 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

Upvotes: 4

Views: 3477

Answers (1)

Amit Badheka
Amit Badheka

Reputation: 2672

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

Upvotes: 3

Related Questions