【问题标题】:Rails pass array to partial using collectionRails使用集合将数组传递给部分
【发布时间】:2013-08-20 03:05:37
【问题描述】:

我正在尝试遍历一个常规数组(不是 ActiveRecord),并使用每个元素呈现一个部分。

在我看来(我使用的是 slim):

== render partial: "layouts/display_elements", collection: my_array

我的部分(目前)仅包含:

= "#{display_element}"

但是,我收到以下错误:

undefined local variable or method `display_element' for #<#<Class:0x007f7fe2e6ca58>:0x007f7fe51e0408>

这是不使用 ActiveRecord 所施加的限制吗?我必须求助于

= my_array.each do |e|

【问题讨论】:

    标签: ruby-on-rails activerecord ruby-on-rails-3.2 slim-lang


    【解决方案1】:

    我对 slim-lang 不是很熟悉,但我认为添加 :as 选项对你有用:

    == render partial: "layouts/display_elements", collection: my_array, as: :display_element
    

    这将允许您访问集合 my_array 作为局部变量中的 display_item 局部变量。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-09-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-04
      • 2012-08-28
      • 2013-01-16
      相关资源
      最近更新 更多