if语句用于开发和生产环境

Sha*_*son 0 ruby-on-rails ruby-on-rails-4

我正在使用Apartment Gem构建一个Rails 4应用程序,我仍然坚持如何继续定义我的邮件程序主机..这是一个教程系列的一部分,在几集之后才停止..

现在最大的问题是这一切都在开发模式下工作,但是当我推送到Heroku,创建一个帐户时,会发送确认电子邮件,但是当我去确认时,确认会将我重定向回我的localhost(lvh.me) :3000)并且生产应用程序未获得确认更新.

我的应用程序控制器中有以下内容:

  def set_mailer_host
    subdomain = current_account ? "#{current_account.subdomain}." : ""
    ActionMailer::Base.default_url_options[:host] = "#{subdomain}lvh.me:3000"
  end
Run Code Online (Sandbox Code Playgroud)

我想添加一个if else这样的声明:

  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
Run Code Online (Sandbox Code Playgroud)

我只是想弄明白,或者我是否应该采取另一种方法来解决这个问题?

Ren*_*Ren 7

您可以根据Rails环境放置条件:

Rails.env.production?

Rails.env.development?

Rails.env.test?
Run Code Online (Sandbox Code Playgroud)

所以在你的代码中,

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
Run Code Online (Sandbox Code Playgroud)

哦,是的,看起来你忘了end你的if-else