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