Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails 5 default_url_options oddities

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

like image 993
Eugene Avatar asked May 02 '17 19:05

Eugene


1 Answers

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.

like image 91
coreyward Avatar answered Sep 28 '22 09:09

coreyward