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

前端之家收集整理的这篇文章主要介绍了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
原文链接:https://www.f2er.com/ruby/271571.html

猜你在找的Ruby相关文章