在Rails 4中格式化我的JSON输出

Pretty format my JSON output in Rails 4

我正在控制器中使用pretty_generate,但我得到以下错误

'只允许生成JSON对象或数组'

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

我不知道为什么会出现这个错误


as the error蓝晶石,only generation of JSON objects or arrays allowed。我想你应该试试这个。P></

1
2
3
4
@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