简体   繁体   中英

if statements for development and production environments

I am building a Rails 4 App using the Apartment Gem and I am stuck on how to proceed with defining my mailer host.. This was part of a tutorial series that was just stopped after a few episodes..

The big issue right now is that this all works in development mode, however when I push to Heroku, create an account, the confirmation email is sent, however when I go to confirm it the confirmation redirects me back to my localhost (lvh.me:3000) and the production app doesn't get the confirmation update.

I have the following in my application controller:

  def set_mailer_host
    subdomain = current_account ? "#{current_account.subdomain}." : ""
    ActionMailer::Base.default_url_options[:host] = "#{subdomain}lvh.me:3000"
  end

I would like to add an if else statement like so:

  def set_mailer_host
   if environment == development
    subdomain = current_account ? "#{current_account.subdomain}." : ""
    ActionMailer::Base.default_url_options[:host] = "#{subdomain}lvh.me:3000"
   else
    subdomain = current_account ? "#{current_account.subdomain}"
    ActionMailer::Base.default_url_options[:host] = "#{subdomain}patrolvault.co"
  end

I just cant figure it out or if I should take another approach to this?

You can place conditions based on the Rails environment with these:

Rails.env.production?

Rails.env.development?

Rails.env.test?

So in your code,

def set_mailer_host
   if Rails.env.development?
    subdomain = current_account ? "#{current_account.subdomain}." : ""
    ActionMailer::Base.default_url_options[:host] = "#{subdomain}lvh.me:3000"
   else
    subdomain = current_account ? "#{current_account.subdomain}"
    ActionMailer::Base.default_url_options[:host] = "#{subdomain}patrolvault.co"
   end
end

Oh yeah, and it looks like you forgot an end in your if-else block

You can use Rails.env.production? methods as described in Ren's post, but imho configuration file for each environment is a much better place.

Take a look at the Configuring Rails Applications section of the official guide. You can use different configuration for every environment by putting related code to the config/environments/[environment name].rb file. For example, if you want to set host for default url options in the production environment, you can put the following code at the end of config/environments/production.rb file (just before the latest end statement):

config.action_mailer.default_url_options = { host: ENV.fetch("DOMAIN") }

Note that this snippet contains another approach. You can put the domain into environment variables and use the same code in the general configuration file. This is even better as you will not have any hardcoded values and repeated code.

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