Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 537
helloworld Avatar asked Jul 15 '16 14:07

helloworld


3 Answers

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
like image 131
Colin B Avatar answered Oct 19 '22 01:10

Colin B


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>
like image 29
Daniel Jose Padilla Peña Avatar answered Oct 19 '22 00:10

Daniel Jose Padilla Peña


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 %>
like image 6
Steve Nims Avatar answered Oct 19 '22 00:10

Steve Nims