0

Why does the test environment need default_url_options be set for ActionMailer?

If I don't set it, I'm getting this when executing my specs:

 ActionView::Template::Error:
   Missing host to link to! Please provide the :host parameter, set default_url_options[:host], or set :only_path to true

I'm especially wondering because when setting

config.action_mailer.default_url_options = {host: 'localhost:3000'}

then in theory links in emails will lead to localhost:3000, which as far as I know is not where the test server instance really is running? Still, when using email_spec gem and clicking on links in emails, they work, because the gem removes the server name and port, so a link typically looks like this:

/en/user/confirmation?confirmation_token=QZu3tw17uozhpEfuVWzF

So one more time: what do I need to specify the host for if it's removed by email_spec gem anyway?

Joshua Muheim
  • 12,617
  • 9
  • 76
  • 152
  • The exception is thrown while [preparing the URL](https://github.com/rails/rails/blob/c8bd12cf5a1dc7ebdb1594334b2598d799d4a746/actionpack/lib/action_dispatch/http/url.rb#L63) – Prakash Murthy Mar 31 '15 at 16:56

1 Answers1

1

You need it because you are generating URLs in your email and ActionMailer needs the :host to know how to properly build the URL. The fact that email_spec removes it later on is irrelevant.

Philip Hallstrom
  • 19,673
  • 2
  • 42
  • 46
  • It's just that I don't find this option in `test.rb` anymore in a newly created Rails 4.2 app, so why do I need to have it set explicitly in my app (which is 4.2, too, but started with 4.1)? – Joshua Muheim Apr 01 '15 at 08:12