【问题标题】:How to add parent attributes inside its children in rabl template?如何在 rabl 模板的子项中添加父属性?
【发布时间】:2013-04-07 18:32:35
【问题描述】:

如何在 rabl 模板的子元素中添加父属性?做这样的事情:

parent: {
  attr_1: 'asd',
  attr_2: 'dfg',
  child: {
    attr_3: 6,
    attr_from_parent: 'cvb'
  }
}

【问题讨论】:

    标签: ruby-on-rails json rabl


    【解决方案1】:
    object @parent
    attributes :attr_1, :attr_2
    
    child :child do
      attributes :attr_3, :name
      node(:attr_from_parent) { @parent.attr_from_parent }
    end
    

    你也可以看看http://railscasts.com/episodes/322-rabl?view=asciicast

    【讨论】:

    • 这不起作用,因为 |parent|等于孩子。还有其他想法吗?
    • 感谢您的回答。这是一个正确的例子,但我把问题简单化了,没有抓住重点。这是我新的更确切的问题:stackoverflow.com/q/15878381/625521感谢您的帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-09-06
    • 1970-01-01
    • 1970-01-01
    • 2012-06-03
    • 2020-03-16
    • 2015-11-13
    • 1970-01-01
    相关资源
    最近更新 更多