Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set action_mailer.default_url_options values in development and test #51191

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 7 additions & 0 deletions railties/CHANGELOG.md
@@ -1,3 +1,10 @@
* Set `action_mailer.default_url_options` values in `development` and `test`.

Prior to this commit, new Rails applications would raise `ActionView::Template::Error`
if a mailer included a url built with a `*_path` helper.

*Steve Polito*

* Introduce `Rails::Generators::Testing::Assertions#assert_initializer`

Compliments the existing `initializer` generator action.
Expand Down
Expand Up @@ -43,6 +43,8 @@ Rails.application.configure do
config.action_mailer.raise_delivery_errors = false

config.action_mailer.perform_caching = false

config.action_mailer.define_url_options = { host: "localhost", port: 3000 }
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Even though we can set this to anything, I felt it was important to keep it consistent with what Rails is served on, in an effort to make previews link to the application.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This sounds like a helpful change—thanks! Quick question: The bin/rails server command allows a --port XXXX flag (and with bin/dev you set it via an env var, e.g. PORT=XXXX bin/dev). Any reason not to use that value, with a default of 3000? I ask because I often use that flag when I have multiple rails apps running.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a great observation. I think we'd somehow need access to the value set in Rails::Commands::Server::ServerCommand:

def port
options[:port] || ENV.fetch("PORT", DEFAULT_PORT).to_i
end

I attempted the following implementation, but if ENV["PORT"] is not set, it won't match what's passed to the --port flag.

config.action_mailer.default_url_options = { host: "localhost", port: ENV.fetch("PORT", 3000).to_i }

However, this seemed to work for the following cases:

  1. When ENV["PORT"] is supplied
  2. When --port is supplied.
  3. When neither ENV["PORT"] nor --port is supplied (defaulting to 3000)
config.action_mailer.default_url_options = { host: "localhost", port: Rails::Server::Options.new.parse!(ARGV)[:Port] }

stevepolitodesign marked this conversation as resolved.
Show resolved Hide resolved
<%- end -%>

# Print deprecation notices to the Rails logger.
Expand Down
Expand Up @@ -44,6 +44,10 @@ Rails.application.configure do
# ActionMailer::Base.deliveries array.
config.action_mailer.delivery_method = :test

# Unlike controllers, the mailer instance doesn't have any context about the
# incoming request so you'll need to provide the :host parameter yourself.
config.action_mailer.default_url_options = { host: "www.example.com" }

<%- end -%>
# Print deprecation notices to the stderr.
config.active_support.deprecation = :stderr
Expand Down