2012-10-26 52 views
0

我正在研究一个RABL JSON feed,我想制作一个可以链接到特定对象的自定义根节点。有没有办法在RABL Feed中创建自定义根节点?

我已经声明,像这样的对象:

object @event 

下面是输出的开头:

{ 
    - event: { 
     id: 131, 

我想拥有它,这样我可以链接到特定对象使用来自“ - 事件”的event_path(event)。有没有办法在RABL中创建一个自定义的根节点?

回答

0

我认为这个post可能会对您的问题有一个答案。以下代码取自博文。

# app/views/users/show.rabl 
object @user 

# Declare the properties to include 
attributes :first_name, :last_name 

# Alias 'age' to 'years_old' 
attributes :age => :years_old 

# Include a custom node with full_name for user 
node :full_name do |user| 
    [user.first_name, user.last_name].join(" ") 
end 

# Include a custom node related to if the user can drink 
node :can_drink do |user| 
    user.age >= 21 
end 
相关问题