在使用Rails电子商务引擎Spree时,我试图将products_path保存在我的Spree::Product模型中以供跟踪。
Looking on SO I found this solution, which looked like the perfect answer.
Rails.application.routes.url_helpers.products_path
Rails.application.routes.url_helpers.products_url(:host => "example.com")但使用该解决方案会给我带来以下错误。
NoMethodError: undefined method `products_path' for #<Module:0x007fe9e208e908>很多挫败感...
发布于 2015-08-21 03:58:34
在使用Rails引擎时,您需要使用引擎的路由助手,而不是主Rails应用程序的路由助手。
Spree::Core::Engine.routes.url_helpers.products_path
Spree::Core::Engine.routes.url_helpers.products_url(:host => "example.com")奖励提示:
如果您想要删除:host => "example.com"参数,可以在环境配置文件中添加主机指示器。
就像url_helpers一样,对于引擎,您需要在引擎路由下添加默认值。
# in config -> environments -> development.rb
Spree::Core::Engine.routes.default_url_options[:host] = 'localhost:3000'注意:此不会为Rails.application.routes.url_helpers设置默认主机;-)
https://stackoverflow.com/questions/32127234
复制相似问题