Rails - '未初始化的常量'在ActionMailer拦截器中使用模型时

时间:2016-06-26 17:52:18

标签: ruby-on-rails ruby-on-rails-4 actionmailer

我正在编写一个小机制来处理电子邮件退回和投诉。 我基本上按照blog post解释了如何实现这种机制。

我正在创建并注册一个ActionMailer拦截器,以确保我要发送的电子邮件在我的应用程序中的模型/表格中没有被标记为退回或投诉。< / p>

# In config/initializers/invalid_email_interceptor.rb

class InvalidEmailInterceptor
  def self.delivering_email(message)
    if EmailResponse.exists? email: message.to
      message.perform_deliveries = false
    end
  end
end

ActionMailer::Base.register_interceptor(InvalidEmailInterceptor)

当我尝试发送电子邮件时,出现以下错误:

  

未初始化的常量InvalidEmailInterceptor :: EmailResponse

我的应用程序似乎在InvalidEmailInterceptor类中寻找EmailResponse而不是我的应用程序的模型。

我尝试使用

的不同变体
MyApp::EmailResponse

require 'email_response'

但我仍然遇到同样的错误,或者甚至无法启动服务器。

有什么想法吗?

谢谢,
西蒙。

1 个答案:

答案 0 :(得分:2)

我不完全确定,但我解决的方法是:

  1. 检查app/models/email_response.rb以确保定义了模型。注意:email_response不是email_responses

  2. 如果是,那么你应该尝试:::EmailResponse,虽然我知道你的模型应该在你的初始化器中可用。