Missing template layouts/mailer with {:locale=>[:en], :formats=>[:html],
Asked Answered
S

6

9

I'm following michael harlt rails tutorial but i get this error

Missing template layouts/mailer with {:locale=>[:en], :formats=>[:html], :variants=>[], :handlers=>[:raw, :erb, :html, :builder, :ruby, :coffee, :jbuilder]}. Searched in: * "/home/ubuntu/workspace/app/views"

when previewing account activation

This is my 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

and the error highlights the line that says

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

I tried adding layout 'mailer' in user_mailer.rb but it doesn't work.

EDIT: Here is the screenshot of the error

The screenshot of my folders

Schlimazel answered 15/7, 2016 at 14:26 Comment(1)
You need to add the actual erb.html (or any other variant you might have)Laocoon
S
2

If you don't already, you will need a folder within views called user_mailer and within that you will need a file for each of those methods (account_activation.html.erb & password_reset.html.erb). This is where the template of your email will be.

Sclerous answered 15/7, 2016 at 15:28 Comment(8)
I have them, all html.erb files in views .Schlimazel
Do you have a github link for this project? or a screenshot of your mailers folder and views where you've placed these files?Sclerous
I'm using bit bucket but i can screenshot it.Schlimazel
Yes please - would help if you could screenshot it. I just want to see the layout of the folders within view and your files in mailer folder.Sclerous
Is the name account_activation or account_activations? of the file which is highlighted?Sclerous
account_activationSchlimazel
i found out that the layout 'mailer' should be 'layout mailer'. Thanks for the help.Schlimazel
@wemode, that is equivalent to commenting out the line. In ruby, any literal value, like "string" or 42 does nothing.Giffin
M
11

I had the same problem, and it seemed to work for me if I commented out the layout 'mailer' line in application_mailer

eg

class ApplicationMailer < ActionMailer::Base
  default from: '[email protected]'
  # layout 'mailer'
end
Margit answered 18/7, 2016 at 2:21 Comment(1)
Any idea why the rails generate command did not produce the layout files for us and it did for Michael Hartl?Equivalent
S
6

The issue is the next:

to generate this mailer you use this command

$rails generate mailer UserMailer account_activation password_reset

sending back

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

by a strange way app/views/layouts/mailer.html.erb file is not generated so when you call

layout 'mailer'

the rails core send you this error

"Missing template layouts/mailer"

you can resolve this by 2 ways.

First: comment out or delete layout 'mailer'.
Second: Creating the file.

Another ways like 'layout mailer' are bad practices cause this syntax have no sense for Rails.

If you create the file, use this code to fill it

<!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>
Summerly answered 11/9, 2016 at 10:38 Comment(2)
Creating a blank file raised an exception. Creating one with just a HTML comment caused blank HTML view output (text worked fine). What content did you have in the file?Giffin
Thanks. Try using backticks - ` to mark text as codeGiffin
M
6

I had this same issue and what resolved it is ensuring I had two files in .../app/views/layouts/: mailer.html.erb and mailer.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 %>
Mainstream answered 17/9, 2016 at 23:34 Comment(3)
What was the content of those files?Giffin
@TomHale, I've edited my response to include to contents of both files.Mainstream
Excellent, this is the solution I went with. I believe it is the only one which works generically in the text-only case - thanks!Giffin
S
2

If you don't already, you will need a folder within views called user_mailer and within that you will need a file for each of those methods (account_activation.html.erb & password_reset.html.erb). This is where the template of your email will be.

Sclerous answered 15/7, 2016 at 15:28 Comment(8)
I have them, all html.erb files in views .Schlimazel
Do you have a github link for this project? or a screenshot of your mailers folder and views where you've placed these files?Sclerous
I'm using bit bucket but i can screenshot it.Schlimazel
Yes please - would help if you could screenshot it. I just want to see the layout of the folders within view and your files in mailer folder.Sclerous
Is the name account_activation or account_activations? of the file which is highlighted?Sclerous
account_activationSchlimazel
i found out that the layout 'mailer' should be 'layout mailer'. Thanks for the help.Schlimazel
@wemode, that is equivalent to commenting out the line. In ruby, any literal value, like "string" or 42 does nothing.Giffin
T
0

Make sure you have the file app/mailers/application_mailer.rb From Hartl's tutorial:

class ApplicationMailer < ActionMailer::Base
 default from: "[email protected]"
 layout 'mailer'
end
Topdress answered 15/7, 2016 at 14:36 Comment(1)
I do have that file with all user_mailers templates in viewsSchlimazel
T
0

Your UserMailer class, is still inheriting from the ApplicationMailer which defines the base layout directory as layout 'mailer'. For your current code and folder structure to work, you could make the following changes.

From this

UserMailer < ApplicationMailer

To this

UserMailer < ActionMailer::Base
Tunis answered 15/1, 2019 at 0:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.