邮件程序错误丢失模板
你好,
当我尝试执行操作时,我遇到了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
Sample mail.
视图/ 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上搜索了解决方案,我尝试了许多类似问题的答案,但不幸的是,它们都没有为我工作。
我找到了解决方案,当我将邮件添加到邮件方法时:
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
我重命名了我的方法并且它有效。 也许该方法的名称不能是邮件或邮件…
class UserMailer < ActionMailer::Base default from: "from@example.com" layout "mailer" def mailer(user) @user = user mail(to: @user.email, subject: 'Test') end end
在布局下添加html布局#
mailer.html.erb
<%= yield %>
在布局下添加文本布局#
mailer.text.erb
<%= yield %>
使用预览根据您的测试框架检查您的电子邮件。
Rails很可能只是找不到你新创建的文件。 如果你有spring
gem运行,这可能是问题。 (更多关于spring: https : //github.com/rails/spring )
要设置终端命令并停止运行spring
:
$ bundle exec spring binstub --all $ bin/spring stop
我尝试了这个,然后用rails s
重新运行我的应用程序。 这不起作用,然后尝试完全关闭终端并重新启动计算机。 运行rails s
并尝试在第二个选项卡中使用rails c
进行测试:
mail = UserMailer.mailer(User.last)
这最终对我有用,希望它能帮助你们中的一些人!
(其中一些步骤可能是多余的。)
我有同样的问题。 重新启动sidekiq为我解决了它。
你能发布一个简约的Github存储库来重现你的错误吗?
您可以尝试生成邮件,以检查您是否忘记了某些内容:
bundle exec rails generate mailer mailer_classname mailer_viewname
在你的情况下:
bundle exec rails generate mailer user_mailer mailer
我有同样的问题,我的解决方案是删除邮件程序模板文件,在您的案例views/user_mailer/mailer.html.erb
再次创建它。 似乎我创建了具有正确名称的文件,但某处有一些奇怪的空白区域,而ActionMailer并没有认出它。