Nil*_*lor 1 ruby json ruby-on-rails rabl
我想像这样创建JSON:
{
"field1": "value-1",
"field2": "value-2",
"actions": {
"edit": "/edit/action/url"
}
}
Run Code Online (Sandbox Code Playgroud)
使用rabl.它不适用于child方法,也不能用它node- 得到未知的方法错误.那么,是否可以使用rabl模板创建自定义子节点?说这样的话:
collection @datas => :datas
attributes :field1, :field2
child :actions do
node :edit do
edit_datas_path :id
end
end
Run Code Online (Sandbox Code Playgroud)
编辑
我选择这个:
node :actions do
actions = {}
actions[:edit] = edit_customer_group_path(:id)
node :foo do
foos = {}
foos[:bar] = "hello"
foos
end
actions
end
Run Code Online (Sandbox Code Playgroud)
但下面接受的答案也是正确的.
Sim*_*eev 13
前几天我偶然发现了这个问题.这张RABL问题的票帮助了我.在您的情况下,我能够使用以下方法生成适当的JSON:
collection @datas
extends "datas/base"
node :actions do
{:edit => root_path}
end
Run Code Online (Sandbox Code Playgroud)
生成JSON:
{
id: 1,
actions: {
edit: "/"
}
}
Run Code Online (Sandbox Code Playgroud)
使用RABL 0.5.3