Today I needed my Rails app to look at it's own production URL and capture the HOST information. After spending an hour looking for an answer, I found this very elegant answer.

heroku config:set HEROKU_URL=$(heroku info -s | grep web_url | cut -d= -f2)

Source

Now from within my Rails app. I can reference the current URL using the environmental variable ENV['HEROKU_URL'].

Here's what config/environments/development.rb looks like.

Rails.application.configure do
  ...
  config.action_mailer.default_url_options = { host: ENV['HEROKU_URL'] }
end