Reputation: 3829
While using Spree, a Rails ecommerce engine, I was trying to save the products_path in my Spree::Product model for tracking.
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")
But using that solution just gave me the following error.
NoMethodError: undefined method `products_path' for #<Module:0x007fe9e208e908>
Much frustration....
Upvotes: 2
Views: 367
Reputation: 3829
When using an Rails Engine, you need to use the Engine's route helpers rather than the main Rails application route helpers.
Spree::Core::Engine.routes.url_helpers.products_path
Spree::Core::Engine.routes.url_helpers.products_url(:host => "example.com")
Bonus tip:
If you want to get rid of the :host => "example.com"
paramater, you can add a host designator in your environment configuration file.
Just like for the url_helpers, for an engine, you will need to add the default under the Engine routes.
# in config -> environments -> development.rb
Spree::Core::Engine.routes.default_url_options[:host] = 'localhost:3000'
Note: this does not set a default host for Rails.application.routes.url_helpers ;-)
Upvotes: 3