簡體   English   中英

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

[英]Pretty format my JSON output in Rails 4

我在我的控制器中使用pretty_generate,但是我收到以下錯誤

'只允許生成一代JSON對象或數組'

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

我不知道為什么我會收到這個錯誤

正如錯誤所示, only generation of JSON objects or arrays allowed 我想你應該試試這個。

@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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM