【问题标题】:Is there a way to make a custom root node in a RABL feed?有没有办法在 RABL 提要中创建自定义根节点?
【发布时间】:2012-10-26 17:19:09
【问题描述】:

我正在处理 RABL JSON 提要,我想创建一个自定义根节点,我可以在其中链接到特定对象。

我有这样声明的对象:

object @event

这里是输出的开始:

{
  - event: {
      id: 131,

我想拥有它,以便我可以使用 event_path(event) 链接到特定对象,其中显示“- event”。有没有办法在 RABL 中制作自定义根节点?

【问题讨论】:

    标签: ruby-on-rails json rabl


    【解决方案1】:

    我认为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
    

    【讨论】: