Rails 5 default_url_options oddities
I have a fairly simple rails app that I am working on upgrading from Rails 4 to Rails 5, but I am noticing something strange with default_url_options
In config/environments/test.rb
I have:
Rails.application.routes.default_url_options[:host]= ENV["HTTP_HOST"] || "localhost"
Rails.application.routes.default_url_options[:port]= ENV["PORT"] || 3000
My application has a namespace api
. In my request specs, I see this:
[1] pry> api_v3_sample_url
=> "http://www.example.com:3000/api/v3/sample"
[2] pry> Rails.application.routes.url_helpers.api_v3_sample_url
=> "http://localhost:3000/api/v3/sample"
What am I missing because these urls are different?
EDIT
Per this thread I have installed
config.action_controller.default_url_options = {
host: ENV['HTTP_HOST'] || 'localhost'
}
in config/environments/test.rb
, but now I get this:
> Rails.application.routes.url_helpers.api_v3_sample_url
ArgumentError: Missing host to link to! Please provide the :host parameter, set default_url_options[:host], or set :only_path to true
> api_v3_sample_url
=> "http://www.example.com/api/v3/sample"
EDIT 2
It might be worth noting that these are request specs, not spec specs (not using capybara).
source to share
This should fix the problem in the controller / request specs:
config.action_controller.default_url_options = {
host: ENV['HTTP_HOST'] || 'localhost'
}
For what this is happening, there is more information available in this current thread on Github .
source to share