1

I've read here Can Rails Routing Helpers (i.e. mymodel_path(model)) be Used in Models? how to include the url helpers in my model. However, when I try to run Rails.application.routes.url_helpers.products_path from within my models, I get a NoMethodError: undefined method products_path for #<Module:0x007fea1d4ffa38>.

The same thing happens, when I try it within a view:

<%= products_path %> # generates /products
<%= Rails.application.routes.url_helpers.products_path %> # raises a NoMethodError

Any idea, why this is happening? I'm on Rails 3.2.9

Update:

This is my routes.rb (the important parts):

Spree::Core::Engine.routes.draw do
  resources :products
end

MyApp::Application.routes.draw do
  class IsCityConstraint
    def matches?(request)
      Spree::CityZone.where(:url => request.params[:city_name]).exists?
    end
  end
  mount Spree::Core::Engine, :at => ':city_name/', :constraints => IsCityConstraint.new, :as => :city
  mount Spree::Core::Engine, :at => '/'
end

So, I have an existing engine, where I add more routes, and this engine gets mounted to / and to /:city_name.

Update:

Here is part of the output of rake routes:

                           products GET      /products(.:format)                                                      spree/products#index
                                    POST     /products(.:format)                                                      spree/products#create
                        new_product GET      /products/new(.:format)                                                  spree/products#new
                       edit_product GET      /products/:id/edit(.:format)                                             spree/products#edit
                            product GET      /products/:id(.:format)                                                  spree/products#show
                                    PUT      /products/:id(.:format)                                                  spree/products#update
                                    DELETE   /products/:id(.:format)                                                  spree/products#destroy
Community
  • 1
  • 1
23tux
  • 12,371
  • 11
  • 76
  • 158

1 Answers1

2

Your products_path inside Spree routes. You can use this:

Spree::Core::Engine.routes.url_helpers.products_path
Alexander Shlenchack
  • 3,401
  • 5
  • 29
  • 45
  • This is kinda the long way. A better one would be to directly call the `spree` routes engine: `spree.products_path`. If you ever define a `products_path` in you main application, you can call it via `main_app.products_path`. – BenMorganIO Oct 12 '15 at 06:03