ruby-on-rails-4.2 - 未调用邮件程序方法?

标签 ruby-on-rails-4.2 ruby-2.1 postmark

内部 Controller :

def update  
  @user.update({approved: true})  
  UserMailer.send_approved_mail(@user)  
  redirect_to(root_url)  
end  

在 user_mailer.rb 里面
class UserMailer < Devise::Mailer  
  def send_approved_mail(user)
    @resource = user
    email_body = render "user_activated_mail"
    if @resource.valid?
     client = Postmark::ApiClient.new(ENV["POSTMARK_API_KEY"])
     client.deliver(from: ENV["POSTMARK_SIGNATURE"], 
      to: @resource.email, subject: "User Activation information.", 
      tag: 'account-activated', :content_type => "text/html",
      html_body: email_body)
    end
  end
end  

在 rails 4.1.0 中, Controller 中的上述方法被调用并发送电子邮件,但在 rails 4.2 中, Controller 中的邮件程序方法未被调用,但是当从 rails 控制台调用时,它可以工作。我已经为 postmark api 和配置文件做了所有必要的配置。唯一的事情是在 Controller 内部的 rails 4.1.0 mailer 中被调用,但在 rails 4.2 中它不会,但是当从 rails 控制台调用时它可以工作。具体是什么原因实在想不通。

最佳答案

您在此处注意到的行为是 Rails 4.2 中引入的新默认值:

With the introduction of Active Job and #deliver_later ... the invocation of the instance methods (on a mailer) is deferred until either deliver_now or deliver_later is called.



查看 Rails 4.2 upgrade guide更多细节。

不过不用担心。您仍然可以将 Postmark 与 ActionMailer 一起使用。 official Postmark Rails gem为 Postmark 提供与 ActionMailer 的直接集成。通过使用它,您应该能够像编写常规 Rails 邮件程序一样编写邮件程序,而无需在代码中手动管理 Postmark 连接。

附言我在 Wildbit(邮戳的创建者)工作。请随时直接与我们联系。

关于ruby-on-rails-4.2 - 未调用邮件程序方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29386753/

相关文章:

ruby-on-rails - 升级到 Rails 4.2 会中断 Delayed::Job:加载作业失败:未定义的方法 'fetch_value'

ruby - ruby 2.1 上的私有(private) def 错误

ruby-on-rails - 与 Ruby 2.0 或 1.9 相比,Ruby 2.1 的内存使用量增加

mustache - mustache : if-statement without scoping - is it possible?

ruby-on-rails - NameError(未初始化常量 Unzipper::Zip)但仅在 Heroku 部署(Rails)上

ruby-on-rails - 为什么 Rails 4.2 + 响应者不断告诉我将响应者添加到 Gemfile?

ruby-on-rails - 我应该使用 rails 4.2 add foreign_key 还是不?

ruby-on-rails - 在 heroku 上找不到字体 (404)

ruby-on-rails - 使用 Postmarkapp 在 Rails 应用程序中处理电子邮件异常的最佳方法是什么?

json - 如何使用 CURL 对 JSON POST 请求中的部分字符进行转义?