当必需节点是属性或方法时,很容易调用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]
答案 0 :(得分:2)
不确定这是否对您有所帮助,但您可以将“本地人”传递给扩展模板,如下所示:
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]