缺少带有{:locale => [:en],:formats => [:html]的模板布局/邮件程序,

问题描述 投票:8回答:6

我正在关注michael hartl rails教程,但是我收到了这个错误

缺少模板布局/邮件使用{:locale => [:en],:formats => [:html],:variants => [],:handlers => [:raw,:erb,:html,:builder,:红宝石,:咖啡,:jbuilder]}。搜索:*“/ home / ubuntu / workspace / app / views”

预览帐户激活时

这是我的user_mailer.rb

class UserMailer < ApplicationMailer

  def account_activation(user)
    @user = user
    mail to: user.email,  subject: "Account activation"
  end

  def password_reset
    @greeting = "Hi"

    mail to: "[email protected]"
  end
end

并且错误突出显示的行

mail to: user.email,  subject: "Account activation"

我尝试在user_mailer.rb中添加layout'mailer',但它不起作用。

编辑:Here is the screenshot of the error

The screenshot of my folders

ruby-on-rails ruby ruby-on-rails-3 web ruby-on-rails-5
6个回答
2
投票

如果您还没有,则需要名为user_mailer的视图中的文件夹,并且您需要为每个方法(account_activation.html.erb和password_reset.html.erb)提供一个文件。这是您的电子邮件模板的位置。


6
投票

我有同样的问题,如果我在application_mailer中注释掉布局'邮件'行,它似乎对我有用

例如

class ApplicationMailer < ActionMailer::Base
  default from: '[email protected]'
  # layout 'mailer'
end

6
投票

问题是下一个:

要生成此邮件程序,请使用此命令

$rails generate mailer UserMailer account_activation password_reset

送回去

create  app/mailers/user_mailer.rb
create  app/mailers/application_mailer.rb
invoke  erb
create    app/views/user_mailer
create    app/views/layouts/mailer.text.erb
create    app/views/layouts/mailer.html.erb
invoke  test_unit
create    test/mailers/user_mailer_test.rb
create    test/mailers/previews/user_mailer_preview.rb

奇怪的是,当你打电话时,app/views/layouts/mailer.html.erb文件没有生成

布局'邮件'

rails core发送给你错误

“缺少模板布局/邮件”

你可以通过两种方式解决这个问题。

第一:注释掉或删除布局'邮件'。 第二:创建文件。

像'布局邮件程序'这样的另一种方法是不好的做法导致这种语法对Rails没有意义。

如果您创建文件,请使用此代码填充它

<!DOCTYPE html>
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <style>
      /* Email styles need to be inline */
    </style>
  </head>

  <body>
    <%= yield %>
  </body>
</html>

6
投票

我有同样的问题,解决它的是确保我在.../app/views/layouts/有两个文件:mailer.html.erbmailer.text.erb

mailer.html.erb

<!DOCTYPE html>
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <style>
      /* Email styles need to be inline */
    </style>
  </head>

  <body>
    <%= yield %>
  </body>
</html>

mailer.text.erb

<%= yield %>

0
投票

确保你有文件app/mailers/application_mailer.rb来自Hartl的教程:

class ApplicationMailer < ActionMailer::Base
 default from: "[email protected]"
 layout 'mailer'
end

0
投票

您的UserMailer类仍然继承自ApplicationMailer,它将基本布局目录定义为layout 'mailer'。要使当前的代码和文件夹结构起作用,您可以进行以下更改。

由此

UserMailer < ApplicationMailer

对此

UserMailer < ActionMailer::Base
© www.soinside.com 2019 - 2024. All rights reserved.