【问题标题】:Create custom child node with RABL使用 RABL 创建自定义子节点
【发布时间】:2012-01-17 21:09:31
【问题描述】:

我想像这样创建 JSON:

{
  "field1": "value-1",
  "field2": "value-2",
  "actions": {
    "edit": "/edit/action/url"
  }
}

使用 rabl。它不适用于 child 方法,我也不能使用 node 来实现 - 出现未知方法错误。那么,是否可以使用 rabl 模板创建自定义子节点?说这样的话:

collection @datas => :datas
attributes :field1, :field2
child :actions do
  node :edit do
    edit_datas_path :id
  end
end

编辑
我选这个:

node :actions do
  actions = {}
  actions[:edit] = edit_customer_group_path(:id)
  node :foo do
    foos = {}
    foos[:bar] = "hello"
    foos
  end
  actions
end

但下面接受的答案也是正确的。

【问题讨论】:

    标签: ruby-on-rails ruby json rabl


    【解决方案1】:

    前几天我偶然发现了这个问题。 RABL 问题中的这个ticket 帮助了我。在您的情况下,我能够使用以下方法生成正确的 JSON:

    collection @datas
    extends "datas/base"
    node :actions do
      {:edit => root_path}
    end
    

    生成的 JSON:

    {
      id: 1,
      actions: {
                edit: "/"
               }
    }
    

    使用 RABL 0.5.3

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-11-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多