ruby-on-railsoopmodel-view-controllerspree

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.

My index.html.erb template :

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

My stores_controller.rb controller :

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

Showing /myapp/app/views/spree/stores/index.html.erb where line #36 raised:

undefined method `each' for # < Class:0x00007fe3f5570a40 >

Line #36 : <% @vendors.each do |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 ?


Solution

  • Spree::Vendor is just a model class. You can think about it like any other model - User, Product, etc.

    In this case, you should call Spree::Vendor.all or Spree::Vendor.active to fetch records.