加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails-4.2 – 没有调用Mailer方法?

发布时间:2020-12-17 03:17:58 所属栏目:百科 来源:网络整理
导读:内部控制器: 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 "use
内部控制器:

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中,正在调用控制器中的方法并且正在发送电子邮件,但是在rails 4.2中,控制器中的邮件程序方法没有被调用,但是当从rails控制台调用时它可以工作.我为postmark api和配置文件做了所有必要的配置.唯一的事情是在rails 4.1.0中,控制器内部的邮件程序被调用,但在rails 4.2中,它从rails console调用时不起作用.究竟是什么原因无法弄明白.

解决方法

您在此处注意到的行为是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邮件程序一样编写邮件程序,而无需在代码中手动管理邮戳连接.

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读