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

Conversation

stevepolitodesign
Copy link
Contributor

Motivation / Background

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

Since we already know new apps will be served on localhost:3000, we set this as the value in development.

In an effort to remain consistent with existing patters, we set the host to www.example.com in test.

Additional information

If we feel this is not the right solution, how do we feel about at least adding these values, but commented out?

Checklist

Before submitting the PR make sure the following are checked:

  • This Pull Request is related to one change. Changes that are unrelated should be opened in separate PRs.
  • Commit message has a detailed description of what changed and why. If this PR fixes a related issue include it in the commit message. Ex: [Fix #issue-number]
  • Tests are added or updated if you fix a bug or add a feature.
  • CHANGELOG files are updated for the changed libraries if there is a behavior change or additional feature. Minor bug fixes and documentation changes should not be included.

@rails-bot rails-bot bot added the railties label Feb 26, 2024
…test`.

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

Since we already know [new apps will be served on `localhost:3000`][new
apps], we set this as the value in `development`.

In an effort to remain consistent with existing patters, we set the
`host` to `www.example.com` in `test.

[new apps]: https://github.com/rails/rails/blob/47300002db11d67d7b35103f5c429dad7dacdacd/README.md?plain=1#L81
@stevepolitodesign stevepolitodesign force-pushed the sp-action-mailer-default-url-options branch from fa72229 to 9412008 Compare February 26, 2024 10:40
@@ -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] }

…onments/development.rb.tt

Co-authored-by: Rafael Mendonça França <rafael@franca.dev>
@rafaelfranca rafaelfranca merged commit 59a3dd4 into rails:main Feb 27, 2024
4 checks passed
@stevepolitodesign stevepolitodesign deleted the sp-action-mailer-default-url-options branch February 28, 2024 01:01
jathayde pushed a commit to jathayde/rails that referenced this pull request Feb 28, 2024
…test` (rails#51191)

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

Since we already know [new apps will be served on `localhost:3000`][new
apps], we set this as the value in `development`.

In an effort to remain consistent with existing patters, we set the
`host` to `www.example.com` in `test.

[new apps]: https://github.com/rails/rails/blob/47300002db11d67d7b35103f5c429dad7dacdacd/README.md?plain=1#L81

* Update railties/lib/rails/generators/rails/app/templates/config/environments/development.rb.tt

Co-authored-by: Rafael Mendonça França <rafael@franca.dev>

---------

Co-authored-by: Rafael Mendonça França <rafael@franca.dev>
viralpraxis pushed a commit to viralpraxis/rails that referenced this pull request Mar 24, 2024
…test` (rails#51191)

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

Since we already know [new apps will be served on `localhost:3000`][new
apps], we set this as the value in `development`.

In an effort to remain consistent with existing patters, we set the
`host` to `www.example.com` in `test.

[new apps]: https://github.com/rails/rails/blob/47300002db11d67d7b35103f5c429dad7dacdacd/README.md?plain=1#L81

* Update railties/lib/rails/generators/rails/app/templates/config/environments/development.rb.tt

Co-authored-by: Rafael Mendonça França <rafael@franca.dev>

---------

Co-authored-by: Rafael Mendonça França <rafael@franca.dev>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants