Kat*_*aro 19 ruby rake ruby-on-rails actionmailer ruby-on-rails-4
你好,
当我尝试执行操作时,我遇到了ActionMailer的问题:
rake send_email
Run Code Online (Sandbox Code Playgroud)
我收到一个错误:
rake aborted!
ActionView::MissingTemplate: Missing template user_mailer/mailer with "mailer". Searched in:
* "user_mailer"
Run Code Online (Sandbox Code Playgroud)
这是我的:
邮寄/ 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
Run Code Online (Sandbox Code Playgroud)
视图/ 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>
Run Code Online (Sandbox Code Playgroud)
视图/ user_mailer文件/ mailer.text.erb
Sample mail.
Run Code Online (Sandbox Code Playgroud)
LIB /任务/ emails_task.rake
desc 'send email'
task send_email: :environment do
UserMailer.mailer(User.last).deliver!
end
Run Code Online (Sandbox Code Playgroud)
配置/环境/ 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
Run Code Online (Sandbox Code Playgroud)
我在stackoverflow上搜索了解决方案,我尝试了许多类似问题的答案,但不幸的是,它们都没有为我工作.
我找到了解决方案,当我向mailer方法添加body时,如:
def mailer(user)
@user = user
mail(to: @user.email, subject: 'Test', body: 'something')
end
Run Code Online (Sandbox Code Playgroud)
然后它确实有效,但我希望在一个单独的文件中有一个正文,并使其与用户名和其他东西更复杂.
如果有人知道如何解决这个问题那么我会非常感激:)
您可以发布一个极简的 Github 存储库来重现您的错误吗?
您可以尝试生成邮件以检查您是否忘记了某些内容:
bundle exec rails generate mailer mailer_classname mailer_viewname
在你的情况下:
bundle exec rails generate mailer user_mailer mailer
| 归档时间: |
|
| 查看次数: |
13010 次 |
| 最近记录: |