Rails - json.erb 模板
我一直在尝试找出一种使用特殊字段、自定义格式等自定义 JSON 的方法。我在模型中创建了一个 as_json 和 to_xml 方法来制定我需要的对象。这很有效,但很草率,因为我的一些辅助方法必须转移到模型中,因为我需要辅助程序和模型中的格式。我还认为这是草率的代码并使模型失控。
我已经能够获得 json.erb 工作的格式,但也不认为它工作 100% 正确,并且回调也不会附加。任何人都能做到这一点
这是我到目前为止所得到的。
api 调用 format.json
模板称为 items.json.erb
<% @items.each do |item| %>
<%= { :item => { :id => item.id, :name => item.name }.to_json.html_safe %>
<% end %>
这有效但看起来很奇怪。有人有建议或有办法做到这一点吗?
顺便说一句,这样做是为了回调工作
<%= params[:callback]+"(" if params[:callback] %>
<% @items.each do |item| %>
<%= { :item => { :id => item.id, :name => item.name }.to_json.html_safe %>
<% end %>
<%= ")" if params[:callback] %>
I have been trying to figure out a way to customize JSON with special fields, custom formats, etc etc. I have created an as_json and to_xml method in my model to formulate the object how I need. This works well but it is sloppy because some of my helper methods had to move into the model, because I need the formats in the helpers and model. I also think it is sloppy code and makes the model out of control.
I have been able to get a format with json.erb working but don't think it is working 100% correct either and the callback doesn't append either. Anyone get this working
Here is what I got so far.
api calls format.json
template called is items.json.erb
<% @items.each do |item| %>
<%= { :item => { :id => item.id, :name => item.name }.to_json.html_safe %>
<% end %>
This works but seems odd. Anyone have suggestions or have a way to do this?
btw did this for the callback to work
<%= params[:callback]+"(" if params[:callback] %>
<% @items.each do |item| %>
<%= { :item => { :id => item.id, :name => item.name }.to_json.html_safe %>
<% end %>
<%= ")" if params[:callback] %>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为最好的方法是跳过
erb
模板(如果出于某种原因您并不绝对需要)。然后你可以这样做:你可以重写模型中的 to_json 方法来添加字段或调用方法。
I think the best way to do this would be to skip the
erb
template if you don't absolutely need if for some reason. Then you could do something like this:You can override the
to_json
method in your model to add fields or call methods.根据您对 Polarblau 的回答,您应该重写 as_json 方法并使用 :methods 参数将方法结果包含在 json 中
Based on your answer to polarblau, you should override the as_json method and use the :methods parameter to include method results in your json
最有可能的是,您会想要:
使用自定义查找器 sql 来更改列名称/执行计算(它比 Ruby 快得多):
MyModel.select('col_name_that_needs_renamed AS new_name').order('some_col DESC')
或更复杂的示例:
MyModel.find_by_sql('SELECT col_name_that_needs_renamed AS new_name, foo_col*50 AS math WHERE foo=bar ORDER some_col LIMIT 8')
如果有些事情你在 SQL 中无法做到(或无法弄清楚),你可以 必须恢复到 Ruby(尽管不推荐,因为它速度明显慢)
to_json 的 API Dock
Most likely, you'll want to either:
use custom finder sql to alter column names/perform calculations (it's much faster than Ruby):
MyModel.select('col_name_that_needs_renamed AS new_name').order('some_col DESC')
or a more complicated example:
MyModel.find_by_sql('SELECT col_name_that_needs_renamed AS new_name, foo_col*50 AS math WHERE foo=bar ORDER some_col LIMIT 8')
if there's something you can't do (or can't figure out) in SQL, you may have to revert to Ruby (although not recommended because it's significantly slower)
API Dock for to_json