2

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....

Community
  • 1
  • 1
Dan Williams
  • 3,769
  • 1
  • 18
  • 26
  • Shouldnt it be `Rails.application.routes.url_helpers.products_path`? Is it a typo? – Amit Badheka Aug 20 '15 at 20:12
  • @AmitBadhekaPykihStaff I don't understand what you mean. What you have typed is identical to what is in the question. Are you asking why doesn't that work? – Dan Williams Aug 20 '15 at 21:28

1 Answers1

2

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 ;-)

Dan Williams
  • 3,769
  • 1
  • 18
  • 26