0
我喜欢添加不同的方式json数据在Rails中呈现。Rails覆盖{:json => @model}的输出
问题是,我不希望每行的所有字段名称,只是一个数组与每列的数据,此外它将很高兴设置某种类型的过滤器,以便您可以选择您的列要在JSON输出
目前我有一个控制器:
respond_to do |format|
format.html # show.html.erb
format.json { render :json => @person }
end
而且我想这样做:
format.json { render :json2 => @person }
因此,在控制器的此特定操作中,JSON数据的格式不同(:json2)。我在看:
ActionController.add_renderer :json2 do |json, options|
但我不能把我的头围绕此。我开始了吗?我希望它能像'默认'的json渲染器一样运行它。所以在每个视图中没有单独的index.json2.erb文件。更妙的是,如果我可以添加一些额外的字段到数组,如:
format.json { render :json2 => @person, :table = 'xyz', :columns = ['a', 'b'] }
我看过Yehuda Katz,但我仍然无法弄清楚。
downvote?为了什么?我确实阅读过其他文章,但那是针对Rails 2的。我刚开始使用Rails 3,并且由于差异... – Roger 2012-03-06 22:06:55