我在找回json的最佳方法, 当“ 展示” 动作在我的控制器里被调用时。 我和铁路公司合作2.3.x。
有些东西我想补充一些东西,有些我想在归还之前从对象中移除,所以只是做其中之一:
respond_to do |format|
format.json {
render :json => @object.to_json
}
end
不一定有效,我不想让那个物体的一切都暴露出来,我以为这里有两只眼:
- Create a private helper method in the controller that builds a new object with only what I want in it, and send that along instead. The problem here is that I have a few views helper methods that I woudn t be able to use with this approach.
- Write out the json manually in a show.json.erb view. Problem here is it seems like I m going against the grain and have to put in lots of logic to account for trailing commas and whatnot.
如果这是 HTML 我回来, 我显然只是使用 html.erb 文件, 并且只包含我在里面想要的东西。 但是既然我和JSON打交道, 我似乎应该处理几乎所有的代码, 甚至不用处理查看文件。
思念?