【问题标题】:is extending views in rails jbuilder possible?是否可以在 rails jbuilder 中扩展视图?
【发布时间】:2013-10-02 20:46:54
【问题描述】:

我想为“parent#show”中的子项编制索引,而每个人首​​先想到的就是使用分部。好吧,那没关系,但是我必须为child#show 创建show.json.jbuilder 和为parent#show 创建_show.json.jbuilder,这不干净。

有什么办法可以解决这个问题吗?我可以扩展视图或类似的东西吗?

【问题讨论】:

    标签: ruby-on-rails jbuilder


    【解决方案1】:

    我遇到了同样的问题。我先创建了 show.json.jbuilder,然后尝试在 index.json.jbuilder 中将其用作部分。

    我的解决方法/解决方案:

    将文件 show.json.jbuilder 移至 _show.json.jbuilder,并用局部变量“mymodel”重命名“@mymodel”的所有实例

    在 show.json.jbuilder 中,我调用了局部变量并将我的实例变量作为本地变量传递:

    json.partial! 'mymodel/show', mymodel: @mymodel

    在 index.json.jbuilder 中,我使用了相同的部分:

    json.array! @mymodel, partial: 'mymodel/show', as: :mymodel

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-02-04
      • 1970-01-01
      • 2021-10-12
      • 2020-12-22
      • 2018-08-16
      • 1970-01-01
      • 1970-01-01
      • 2010-12-21
      相关资源
      最近更新 更多