gpt4 book ai didi

ruby-on-rails - 邮件错误缺少模板

转载 作者:数据小太阳 更新时间:2023-10-29 06:32:20 29 4
gpt4 key购买 nike

你好,

当我尝试执行操作时,ActionMailer 出现问题:

rake send_email

我得到一个错误:

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

这是我的:

ma​​ilers/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

views/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>

views/user_mailer/mailer.text.erb

Sample mail.

lib/tasks/emails_task.rake

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

config/environments/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 上搜索了解决方案,并针对类似问题尝试了很多答案,但不幸的是,没有一个对我有用。

我找到了解决方案,当我将正文添加到邮件方法时:

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

关于ruby-on-rails - 邮件错误缺少模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30984400/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com