0

当需要的节点是属性或方法时,很容易调用extends。

但是当它不是一个属性时呢?它可以是实例变量或 attr 访问器。

下面的例子:


// dispatches is an attribute of the class
child :dispatches => :dispatches do
  extends "/dispatches/_base"
end

# completed post is not an attribute of the class
if @completed_post
  node(:post) do |post|
    {
      id: @completed_post.id.to_s,
      _id: @completed_post.id.to_s,
      display_text: @completed_post.display_text,
      # and many others
    }
end

这需要的附加节点可以是@instance 或@object[:some_attribute]

4

1 回答 1

2

不确定这是否会对您有所帮助,但您可以将“locals”传递给扩展模板,如下所示:

extends('posts/show', :locals => { :hide_comments => true })

然后在您的posts/show模板中:

# app/views/posts/show.json.rabl
object @post

node(:comments) { |post| post.comments } unless locals[:hide_comments]
于 2013-09-08T08:54:41.187 回答