【问题标题】:Rails ActionMailer User Mailer No MethodRails ActionMailer 用户邮件程序无方法
【发布时间】:2020-06-16 14:22:42
【问题描述】:

我没有让我的 user_mailer 工作。它说没有方法,我找不到原因:

来自 Rails:

NoMethodError in AtendimentosController#create
undefined method `welcome_email' for UserMailer:Module

来自 Rails C(手动测试):

irb(main):027:0> ActionMailer::usermailer.send_welcome_email(from: "test@example.co", to: '...@gmail.com', subject: 'subjecto de teste', body: "Test")
Traceback (most recent call last):
        2: from (irb):27
        1: from (irb):27:in `rescue in irb_binding'
NoMethodError (undefined method `usermailer' for ActionMailer:Module)

类 UserMailer 位于 /mailers/user_mailer/user_mailer.rb :

class UserMailer < ApplicationMailer
  def welcome_email(user)
    mail(:to => @user.email, :subject => "Welcome!")
  end
end

我正在呼叫控制器:

  def send_mail
    UserMailer.welcome_email(self).deliver_later
  end     

【问题讨论】:

  • 你在哪里打电话send_mail?为什么要将self(控制器)传递给邮件程序类?我怀疑你想通过发送用户对象。此外,@user 在您发布的 welcome_email 方法中不存在。如果您在视图中使用它,则需要将其实例化为 @user = user,或者只使用 user.email 作为 :to =&gt; 参数。
  • 您好@scilence,感谢您的反馈。正如您所说,我已经尝试更改并包含使用 Rails C 的手动事务,但由于“无方法”而仍然无法正常工作。我从下面的链接中获取的代码示例,但正如我所说,我已经尝试更改甚至执行手动 ActiveEmail 工作。 altalogy.com/blog/rails-6-user-accounts-with-3-types-of-roles

标签: ruby-on-rails-5 actionmailer


【解决方案1】:

您应该将此课程移至/mailers/user_mailer.rb。将文件放在单独的文件夹中时,文件必须以Module ModuleName 开头。因此,在您的示例中,当您将此文件放入 /mailers/user_mailer/user_mailer.rb 时,它应该如下所示:

module UserMailer
  class UserMailer < ApplicationMailer
    def welcome_email(user)
      mail(:to => @user.email, :subject => "Welcome!")
    end
  end
end

然后你叫它:UserMailer::UserMailer.welcome_email(self).deliver

【讨论】:

    猜你喜欢
    • 2011-08-11
    • 1970-01-01
    • 1970-01-01
    • 2015-04-22
    • 2013-02-12
    • 2011-07-10
    • 2015-01-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多