ruby-on-rails – Mailer错误缺失的模板

你好,

我有ActionMailer的问题,当我尝试执行操作:

rake send_email

我收到一个错误

rake aborted!
ActionView::MissingTemplate: Missing template user_mailer/mailer with "mailer". Searched in:
  * "user_mailer"

这是我的

邮寄/ user_mailer.rb

class UserMailer < ActionMailer::Base
  default from: "from@example.com"

  def mailer(user)
    @user = user
    mail(to: @user.email,subject: 'Test')
  end

end

视图/ user_mailer文件/ mailer.html.erb

<!DOCTYPE html>
<html>
  <head>
    <Meta content='text/html; charset=UTF-8' http-equiv='Content-Type' />
  </head>
  <body>
    <p>
      Sample mail.
    </p>
  </body>
</html>

视图/ user_mailer文件/ mailer.text.erb

Sample mail.

LIB /任务/ emails_task.rake

desc 'send email'
task send_email: :environment do
  UserMailer.mailer(User.last).deliver!
end

配置/环境/ development.rb

# I recommend using this line to show error
  config.action_mailer.raise_delivery_errors = true

  # ActionMailer Config
  config.action_mailer.delivery_method = :letter_opener

# config.action_mailer.default_url_options = { :host => 'localhost:3000' }
# config.action_mailer.delivery_method = :smtp
# SMTP settings for gmail
config.action_mailer.smtp_settings = {
 :address              => "smtp.gmail.com",:port                 => 587,:user_name            => ENV['gmail_username'],:password             => ENV['gmail_password'],:authentication       => "plain",:enable_starttls_auto => true
}

# Send email in development mode?
config.action_mailer.perform_deliveries = true

我在stackoverflow上搜索解决方案,我尝试了许多类似问题的答案,但不幸的是他们没有为我工作.

我找到解决方案,当我添加body的邮件方法像:

def mailer(user)
  @user = user
  mail(to: @user.email,subject: 'Test',body: 'something')
end

然后它的工作,但我想要一个单独的文件,并使其更复杂的用户名和其他的东西.

如果有人有一个想法如何解决这个问题,那么我会很感激:)

解决方法

尝试添加布局
class UserMailer < ActionMailer::Base
  default from: "from@example.com"

  layout "mailer"

  def mailer(user)
   @user = user
   mail(to: @user.email,subject: 'Test')
 end

end

相关文章

validates:conclusion,:presence=>true,:inclusion=>{...
一、redis集群搭建redis3.0以前,提供了Sentinel工具来监控各...
分享一下我老师大神的人工智能教程。零基础!通俗易懂!风趣...
上一篇博文 ruby传参之引用类型 里边定义了一个方法名 mo...
一编程与编程语言 什么是编程语言? 能够被计算机所识别的表...
Ruby类和对象Ruby是一种完美的面向对象编程语言。面向对象编...