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

How do I get a constant recognized in my mailer file?

使用 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 => 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

我该如何解决这个问题?

您不需要在常量前加上模块名称前缀。该常量将在全局范围内可用。

邮箱:

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

config/initializers/global.rb

EMAIL_CONFIRMATION_SUBJECT = "Please confirm your email."

尽管如此,我还是建议您使用 Rails 中的国际化,因为它就是为此目的而构建的 http://guides.rubyonrails.org/i18n.html