我在控制器中使用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
我不确定为什么会出现这个错误
@celebrities.to_json(:include =>{:category => {:only => [:category]} })
,将结果存储在一个变量中,然后调用JSON.pretty_generate
等。 - Max Williamsrequire 'json'
。 - Max Williams