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


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
链接地址: http://www.djcxy.com/p/47158.html

上一篇: 活动模型串行器格式化JSON显示

下一篇: 在Rails 4中格式化我的JSON输出