From 5b89f64eaf234c1338a3d1a4bf0fb574bf60fbe9 Mon Sep 17 00:00:00 2001 From: Andrew Kvalheim Date: Thu, 27 Oct 2022 12:54:35 -0700 Subject: [PATCH] Set default URL host MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Setting the default host resolves: Missing host to link to! Please provide the :host parameter, set default_url_options[:host], or set :only_path to true Configuring RSpec to use the default host resolves: Expected response to be a redirect to but was a redirect to . --- config/environments/development.rb | 6 ++++-- config/environments/production.rb | 7 ++++++- config/environments/test.rb | 8 ++++++-- spec/spec_helper.rb | 5 +++++ 4 files changed, 21 insertions(+), 5 deletions(-) diff --git a/config/environments/development.rb b/config/environments/development.rb index 0397ea88..55daeeda 100644 --- a/config/environments/development.rb +++ b/config/environments/development.rb @@ -54,8 +54,10 @@ Rails.application.configure do # Annotate rendered view with file names. # config.action_view.annotate_rendered_view_with_filenames = true - # Set the detault url for action mailer - config.action_mailer.default_url_options = { host: ENV.fetch('OSEM_HOSTNAME', 'localhost:3000') } + # Provide a default host for URLs + Rails.application.routes.default_url_options[:host] = ENV.fetch('OSEM_HOSTNAME', 'localhost:3000') + config.action_controller.default_url_options = Rails.application.routes.default_url_options + config.action_mailer.default_url_options = Rails.application.routes.default_url_options # Access all mails sent at http://localhost:3000/letter_opener config.action_mailer.delivery_method = :letter_opener diff --git a/config/environments/production.rb b/config/environments/production.rb index a301acc8..77c3b8ff 100644 --- a/config/environments/production.rb +++ b/config/environments/production.rb @@ -73,7 +73,12 @@ Rails.application.configure do config.logger = ActiveSupport::TaggedLogging.new(logger) end - config.action_mailer.default_url_options = { host: ENV.fetch('OSEM_HOSTNAME', 'localhost:3000') } + # Provide a default host for URLs + Rails.application.routes.default_url_options[:host] = ENV.fetch('OSEM_HOSTNAME', 'localhost:3000') + config.action_controller.default_url_options = Rails.application.routes.default_url_options + config.action_mailer.default_url_options = Rails.application.routes.default_url_options + + # Configure outgoing mail config.action_mailer.smtp_settings = { address: ENV.fetch('OSEM_SMTP_ADDRESS', 'localhost'), port: ENV.fetch('OSEM_SMTP_PORT', 25), diff --git a/config/environments/test.rb b/config/environments/test.rb index 25a85f43..44b816eb 100644 --- a/config/environments/test.rb +++ b/config/environments/test.rb @@ -43,8 +43,12 @@ Rails.application.configure do # Tell Active Support which deprecation messages to disallow. config.active_support.disallowed_deprecation_warnings = [] - # Set the detault url for action mailer - config.action_mailer.default_url_options = { host: 'localhost:3000' } + # Provide a default host for URLs + Rails.application.routes.default_url_options[:host] = 'localhost:3000' + config.action_controller.default_url_options = Rails.application.routes.default_url_options + config.action_mailer.default_url_options = Rails.application.routes.default_url_options + + # Configure outgoing mail config.action_mailer.delivery_method = :test config.after_initialize do diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 59310381..d5330db5 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -117,6 +117,11 @@ RSpec.configure do |config| # end # end + # Expect configured host instead of `test.host` (see https://stackoverflow.com/q/15414847) + config.before(:each, type: :controller) do + @request.host = Rails.application.routes.default_url_options[:host] + end + # use the config to use # t('some.locale.key') instead of always having to type I18n.t config.include AbstractController::Translation