简体   繁体   中英

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

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM