Rails & Devise: how to configure mail with a domain name automatically?

I need tips on setting up mail on Ruby-on-Rails sites.

I am deploying a Rails application on EngineYard. I have several sites, such as demo.mydomain.com or staging.mydomain.com - how can I configure Devise so that during deployment I can make sure that confirmation messages are received from demo.mydomain.com or staging.mydomain.com automatically? those. I want to have the same GitHub codebase and want to dynamically populate the configuration.

Currently in config/environments/production.rb I have a line:

 config.action_mailer.default_url_options = { :host => 'demo.mydomain.com' } 

But this is not true when the same code is deployed to staging.mydomain.com , since both of them work in RAILS_ENV=production

Any ideas?

Thanks Dave

Update:. Now, to be practical, I have added specific environments for hard coding the domain of the mailer. So now demo.mydomain.com runs on environments/demo.rb , and www.mydomain.com runs on environments/productions.rb . What I don't like about this is the duplication between files, I don’t understand how to DRY them, like mine, for example, database.yml

+4
source share
3 answers

in your development configuration, usually config/initializers/devise.rb you can configure the sender for development. this configuration accepts proc so that you can evaluate something at runtime.

 Devise.setup do |config| config.mailer_sender = Proc.new { your_magic_here } end 
+4
source

Ideally, mid-tier and production servers should run in different rail environments. However, if you want your production server to work on both intermediate and production servers with different mailing list URLs, this must be done at the deployment level. You can always write an environment file during deployment.

+1
source

First of all, I think you should separate the environment of your application. Check out this guide to find out how you can do this.

Then try something like this in your configuration:

 Devise.setup do |config| if Rails.env.production? config.mailer_sender = " no-reply@domain.com " elsif Rails.env.staging? config.mailer_sender = " no-reply@staging.domain.com " else config.mailer_sender = " no-reply@domain.com " end ... 

Check the guide to learn more about the Proc object.

0
source

Source: https://habr.com/ru/post/1393775/


All Articles