加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby – 将任意选项传递给rabl partial / extend

发布时间:2020-12-17 02:02:31 所属栏目:百科 来源:网络整理
导读:是否有可能将除了对象之外的选项传递给rabl部分? 例如,父模板将名为“show_field1”的选项传递给基本模板: extends "base",:show_field1 = true 然后在base.rabl中可以使用这样的选项: attribute :field1 if @show_field1 我能够选择的唯一方法是通过这种
是否有可能将除了对象之外的选项传递给rabl部分?

例如,父模板将名为“show_field1”的选项传递给基本模板:

extends "base",:show_field1 => true

然后在base.rabl中可以使用这样的选项:

attribute :field1 if @show_field1

我能够选择的唯一方法是通过这种可怕的方式来寻找隐藏的方法:

attribute :field1 if @_options[:show_field1]

解决方法

RABL通过使用特殊的本地哈希来支持这一点:

# some_view.rabl
extends "base",locals: { show_field1: true }

# base.rabl
attribute :field1 if locals[:show_field1]

RABL Documentation on the subject

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读