简体   繁体   中英

Disable mailer for specific environment or remote in rails

In my rails app I have configured three different environments/remotes for heroku:

  • production (www.myapp.com)
  • staging (staging-myapp.herokuapp.com)
  • playground (playground-myapp.herokuapp.com)

On my playground environment I don't want to send email notifications at all so I somehow need to tell my production.rb that it should ignore email-sending on this environment.

So far I have accessed the environment information via request.subdomain but I don't believe that this is a good best practice. Also, request.subdomain is not available in models or mailers, so it's kind of useless longterm.

What are the best practices here to configure my rails app based on the different remotes I use?

Another usecase would be to set a passwort if I am on staging for example. So far I do this via application controller and a simple hack:

if request.subdomains.first == 'staging-myapp'
  authenticate_or_request_with_http_basic do |username, password|
      username == "user123" && password == "12345"
  end
end

You could add a new environment "playground" and set the mailer settings to what you'd like.

Check How to create a new environment in Ruby on Rails? for a detailed description on how to create a new environment.

Set up different config variables for each remote, and access them in your code with the same references eg

authenticate_or_request_with_http_basic do |username, password|
  username == ENV['USERNAME'] && password == ENV['PASSWORD']
end

So for each remote you would do:

heroku config:set USERNAME=productionpassword --app production-app
heroku config:set USERNAME=stagingpassword --app staging-app
heroku config:set USERNAME=playground --app playground-app

In the same way you can specify config vars in your production.rb file (for example for your Mailer settings), so you don't need to have a separate staging.rb and playground.rb when using Heroku. You can read more about this in the Heroku documentation here and here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM