Rails 4控制器不会调用我的动作管理器类

时间:2015-11-30 07:24:46

标签: ruby-on-rails ruby actionmailer

在我的rails 4.2.5(ruby 2.2.1)应用程序中,我有一个非常简单的actionmailer类。它甚至不发送邮件,只是printf:

Switch-AzureMode AzureResourceManager

但是当我的控制器调用此函数时,它从不执行printf!

class UserTommail < ActionMailer::Base

  def joe
  printf("\n***** In Emails.joe")
  end

end

控制器中的两个def contact_us printf("\n***** TOMS EMAILLER") UserTommail.joe() redirect_to(root_path(), :notice => "Your Contact Us message has been successfully sent.") printf("\n**** TOMS END") end 实际上打印了他们的消息,但joe()中的消息却从未这样做过。没有错误或任何东西。

如果我在文件printf中破坏joe()来表示joe(),我会收到一个错误,找不到该函数,所以我知道控制器知道它。

我做错了什么?

3 个答案:

答案 0 :(得分:1)

您应致电deliver_now以解雇邮件发送电子邮件:

UserTommail.joe.deliver_now

See the full list of available methods in docs

答案 1 :(得分:0)

让Rails 4发送电子邮件

UserTommail.joe.deliver_now

UserTommail.joe.deliver_later #Enqueues the email to be delivered through Active Job. When the job runs it will send the email using deliver_now.

让Rails 3发送电子邮件

UserTommail.joe.deliver

答案 2 :(得分:0)

这里的问题是ActionMailer试图变得聪明&#34;关于它的作用,并且实际上不会调用你的邮件程序方法,直到需要它的返回值。

您的邮件程序方法joe将返回一个ActionMailer::MessageDelivery对象,其中包含Mail::Message个对象(即使您没有明确表示要发送电子邮件)。 Mail::Message被懒惰地评估,这意味着它不会被实例化(并且您的方法不会被调用),直到需要它为止。

强制进行评估的一种方法是尝试使用deliver_nowdeliver_later发送返回的电子邮件,但另一种方法就是检查邮件。

如果你有my_email = UserTommail.joe()然后调用my_email.message,它会强制运行该方法,你会在控制台中看到你的printf