Rails.application.routes.url_helpers 缺少方法
Rails.application.routes.url_helpers missing method
我在这里 Can Rails Routing Helpers (i.e. mymodel_path(model)) be Used in Models? 阅读了如何在我的模型中包含 url 助手。但是,当我尝试从我的模型中 运行 Rails.application.routes.url_helpers.products_path
时,我得到 NoMethodError: undefined method products_path for #<Module:0x007fea1d4ffa38>
.
当我在一个视图中尝试时,同样的事情发生了:
<%= products_path %> # generates /products
<%= Rails.application.routes.url_helpers.products_path %> # raises a NoMethodError
知道为什么会这样吗?我在 Rails 3.2.9
更新:
这是我的routes.rb
(重要部分):
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
所以,我有一个现有的引擎,我在其中添加了更多路线,并且该引擎安装到 /
和 /:city_name
。
更新:
这是 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
您的 products_path
内部 Spree
路线。你可以使用这个:
Spree::Core::Engine.routes.url_helpers.products_path
我在这里 Can Rails Routing Helpers (i.e. mymodel_path(model)) be Used in Models? 阅读了如何在我的模型中包含 url 助手。但是,当我尝试从我的模型中 运行 Rails.application.routes.url_helpers.products_path
时,我得到 NoMethodError: undefined method products_path for #<Module:0x007fea1d4ffa38>
.
当我在一个视图中尝试时,同样的事情发生了:
<%= products_path %> # generates /products
<%= Rails.application.routes.url_helpers.products_path %> # raises a NoMethodError
知道为什么会这样吗?我在 Rails 3.2.9
更新:
这是我的routes.rb
(重要部分):
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
所以,我有一个现有的引擎,我在其中添加了更多路线,并且该引擎安装到 /
和 /:city_name
。
更新:
这是 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
您的 products_path
内部 Spree
路线。你可以使用这个:
Spree::Core::Engine.routes.url_helpers.products_path