Reputation: 4879
I have a pretty simple rails app that I'm working on upgrading from Rails 4 to Rails 5, but I'm noticing some weirdness 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 called api
. In my request specs, I'm seeing 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 that is causing those URLs to be different?
EDIT
Per this thread I set
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
Probably worth noting that these are request specs and not feature specs (not using capybara).
Upvotes: 6
Views: 7813
Reputation: 80041
This should fix the issue in controller/request specs:
config.action_controller.default_url_options = {
host: ENV['HTTP_HOST'] || 'localhost'
}
As for why this is occurring, there's some more information available in this ongoing thread on Github.
Upvotes: 3