简体   繁体   English

Spree Commerce供应商列表页[NoMethodError:#的未定义方法&#39;each&#39; <Class:…> ]

[英]Spree Commerce Vendors Listing Page [NoMethodError : undefined method `each' for #<Class:…>]

I'm creating a view that list all vendors using Spree Multi Vendor extension for Spree Commerce . 我正在创建一个视图,列出所有使用Spree Commerce的 Spree Multi Vendor扩展的供应

My index.html.erb template : 我的index.html.erb模板:

<% @vendors.each do |vendor| %>
    <h2>
      <%= vendor.name %>
    </h2>
<% end %>

My stores_controller.rb controller : 我的stores_controller.rb控制器:

module Spree
    class StoresController < Spree::StoreController  
      def index
        @vendors = Spree::Vendor
      end
    end
end

The error that I'm getting : 我得到的错误:

NoMethodError in Spree::Stores#index Spree :: Stores#index中的NoMethodError

Showing /myapp/app/views/spree/stores/index.html.erb where line #36 raised: 显示/myapp/app/views/spree/stores/index.html.erb,其中第36行出现:

undefined method `each' for # < Class:0x00007fe3f5570a40 > #<Class:0x00007fe3f5570a40>的未定义方法'each'

Line #36 : <% @vendors.each do |vendor| 第36行:<%@ vendors.each做| vendor | %> %>

How can I correctly instantiate Vendor Model inside my controller so I can use it's methods and access it's attributes in the views of my store ? 如何在控制器内部正确实例化Vendor Model以便可以在商店的视图中使用它的方法并访问其属性?

Spree::Vendor is just a model class. Spree::Vendor只是一个模型类。 You can think about it like any other model - User , Product , etc. 您可以像其他任何模型一样思考它UserProduct等。

In this case, you should call Spree::Vendor.all or Spree::Vendor.active to fetch records. 在这种情况下,您应该调用Spree::Vendor.allSpree::Vendor.active来获取记录。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM