3

我在这里做一些明显错误的事情,但我不明白。

我有一个像这样的部分调用:

= render 'shared/purchase', collection: @purchases

@purchases在控制器中定义如下:

@purchases = current_user.purchases

但我得到这个错误:

app/views/shared/_purchase.html.slim where line #3 raised:

undefined local variable or method `purchase' for #<<Class:0x007fd8ca54f970>:0x007fd8ca54eb60>

但是,当我将部分更改为仅渲染 local_assigns 时,我看到了:

[:collection, #<ActiveRecord::Associations::CollectionProxy [#<Purchase id: 40, user_id: 20, purchaseable_id: 6, purchaseable_type: "PurchaseType", frequency: "Weekly", day: "Su", notes: "leave at gate", allergies: "peanuts", created_at: "2013-07-24 16:58:08", updated_at: "2013-07-24 16:58:08", size: "Normal", quantity: 2>, #<Purchase id: 41, user_id: 20, purchaseable_id: 5, purchaseable_type: "PurchaseType", frequency: "Weekly", day: "Su", notes: "leave at gate", allergies: "peanuts", created_at: "2013-07-24 16:58:08", updated_at: "2013-07-24 16:58:08", size: "Jumbo", quantity: 3>, #<Purchase id: 42, user_id: 20, purchaseable_id: 7, purchaseable_type: "PurchaseType", frequency: "Weekly", day: "Su", notes: "leave at gate", allergies: "peanuts", created_at: "2013-07-24 16:58:08", updated_at: "2013-07-24 16:58:08", size: nil, quantity: 1>, #<Purchase id: 43, user_id: 20, purchaseable_id: 8, purchaseable_type: "PurchaseType", frequency: "Weekly", day: "Su", notes: "leave at gate", allergies: "peanuts", created_at: "2013-07-24 16:58:08", updated_at: "2013-07-24 16:58:08", size: nil, quantity: 1>]>]
[:purchase, nil]

为什么不是每个集合项都作为局部变量被拾取purchase

我把头发扯掉了,我确定这很愚蠢,我以前做过很多次,甚至将旧的工作代码与这个进行比较,我无法弄清楚问题是什么。要么是我在文档中没有看到的新 Rails 4 东西,要么我是个白痴。

谢谢!

4

1 回答 1

10

Rails 文档中的Layouts and Render in Rails给出了一个使用partial参数 for的示例render

<h1>Products</h1>
<%= render partial: "product", collection: @products %>
于 2013-07-26T19:22:00.643 回答