【问题标题】:ActionMailer Class not getting inherited in Rails AppActionMailer 类没有在 Rails 应用程序中被继承
【发布时间】:2018-02-09 15:14:42
【问题描述】:

我正在开发一个没有项目结构视图的 Ruby on Rails 4.2.10 应用程序。它是微服务架构中的后端逻辑。

我在这里遇到的问题是 ActionMailer 类没有被子类继承。

电子邮件是以前发送的。现在,同样的实现不起作用。 这与 Rails 版本有什么关系吗? 请帮忙。

application_mailer.rb

class ApplicationMailer < ActionMailer::Base
  default from: "from@example.com"
  layout 'mailer'
end

sample_mailer.rb

class SampleMailer < ApplicationMailer

    def  xlsx_email(email, xlsx, filename)
         mail.attachments[filename] = xlsx.read
         body = "The  report you requested is attached.\n\nThanks "
         mail(from: "noreply@sample.com", to: email, subject: "Report ", body: 
          body)
    end

end

我得到的错误是

NameError: SampleMailer:Class 的未定义局部变量或方法“邮件”

当我尝试使用邮件和附件方法直接使用 ActionMailer::Base 调用时,它显示错误

ActionView::MissingTemplate:缺少带有“mailer”的模板 sample_mailer/xlsx_email。搜索:* "sample_mailer"

【问题讨论】:

  • 尝试用attachments[filename] = xlsx.read替换xlsx_email方法的第一行

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


【解决方案1】:

如果您不提供响应格式,那么您的代码将查找模板页面是否存在。 “app/views/sample_mailer/xlsx_email.html.erb”。

对于后一个问题,如果不想渲染模板,可以尝试关注。

mail(from: "noreply@sample.com", to: email, subject: "Report ", body: 
      body) do |format|
    format.text { render plain: "Hello Mikel!" }
end

查看此链接了解更多信息。 https://apidock.com/rails/ActionMailer/Base/mail

【讨论】:

    【解决方案2】:

    mail方法中使用&amp;:html,如下所示。

    class SampleMailer < ApplicationMailer
    
      def xlsx_email(email, xlsx, filename)
        mail.attachments[filename] = xlsx.read
        body = "The  report you requested is attached.\n\nThanks "
        mail(from: "noreply@sample.com", to: email, subject: "Report ", body: body, &:html)
      end
    end
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-10-01
      • 2022-12-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-21
      相关资源
      最近更新 更多