如何在我的邮件文件中识别常量?

时间:2017-09-11 20:32:06

标签: ruby-on-rails ruby-on-rails-5 constants mailer

使用Rails 5,如何在我的邮件文件中识别常量?我有这个文件,app / mailers / user_notifier.rb,

class UserNotifier < ActionMailer::Base
    ...

  # send notification email to user about the price
  def send_confirmation_email(user_id)
    @user = User.find(user_id)
    mail( :to => @user.email,
    :subject => Constants::EMAIL_CONFIRMATION_SUBJECT )
  end

end

但当它到达该行时,“:subject =&gt; Constants :: EMAIL_CONFIRMATION_SUBJECT”“它因错误而死亡

    uninitialized constant Constants::EMAIL_CONFIRMATION_SUBJECT

尽管我在config / initializers / global.rb文件中定义了常量

module Constants

  # Subject for email confirmations
  EMAIL_CONFIRMATION_SUBJECT = "Please confirm your email."

end

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

您不需要在常量前加上模块名称。该常量将在全球范围内提供。

电子邮件:

def send_confirmation_email(user_id)
 @user = User.find(user_id)
 mail( :to => @user.email,
  :subject => EMAIL_CONFIRMATION_SUBJECT )
end

配置/初始化/ global.rb

EMAIL_CONFIRMATION_SUBJECT = "Please confirm your email."

虽然,我建议您在Rails中使用国际化,因为它是为此目的而构建的http://guides.rubyonrails.org/i18n.html