【问题标题】:Can you pass extra variables when rendering a collection in Rails?在 Rails 中渲染集合时可以传递额外的变量吗?
【发布时间】:2019-03-21 12:27:28
【问题描述】:

我在最近的一个项目中经常使用这种模式:

<%= render partial: 'path/to/partial', collection: @crowd, as: :person %>

我想知道是否可以使用相同的语法设置另一个局部变量(而不是使用显式的 @crowd.each 循环),所以它看起来像这样:

<%= render partial: 'path/to/partial', collection: @crowd, as: :person, day: 'Thursday' %>

(以上没有错误,只是没有在相关部分分配day

【问题讨论】:

  • 尝试将day 包装在locals 哈希中。 locals: { day: 'Thursday' }
  • 试试:&lt;%= render partial: 'path/to/partial', collection: @crowd, as: :person, locals: { day: 'Thursday' } %&gt;guides.rubyonrails.org/…

标签: ruby-on-rails actionview


【解决方案1】:

locals

render partial: 'some_partial', collection: @collection, as: :item_name, locals: { variable_name: 'value', other_variable: 123 }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多