Rails - json.erb 模板

发布于 2024-10-25 05:17:39 字数 789 浏览 1 评论 0原文

我一直在尝试找出一种使用特殊字段、自定义格式等自定义 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

阪姬 2024-11-01 05:17:39

我认为最好的方法是跳过 erb 模板(如果出于某种原因您并不绝对需要)。然后你可以这样做:

items = Item.all
render :json => items.to_json(:only => [:id, :name]), :callback => params[:callback]

你可以重写模型中的 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:

items = Item.all
render :json => items.to_json(:only => [:id, :name]), :callback => params[:callback]

You can override the to_json method in your model to add fields or call methods.

海的爱人是光 2024-11-01 05:17:39

根据您对 Polarblau 的回答,您应该重写 as_json 方法并使用 :methods 参数将方法结果包含在 json 中

class Item

  def date
    return "1 year and 8 months" #obviously use logic here
  end

  def as_json(args={})
    super(:methods=>[:date], :only=>[:id=>:name])
  end
end

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

class Item

  def date
    return "1 year and 8 months" #obviously use logic here
  end

  def as_json(args={})
    super(:methods=>[:date], :only=>[:id=>:name])
  end
end
奢华的一滴泪 2024-11-01 05:17:39

最有可能的是,您会想要:

  1. 使用自定义查找器 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')

  2. 如果有些事情你在 SQL 中无法做到(或无法弄清楚),你可以 必须恢复到 Ruby(尽管不推荐,因为它速度明显慢)

    to_json 的 API Dock

Most likely, you'll want to either:

  1. 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')

  2. 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文