Ben Downey
Ben Downey

Reputation: 2665

How to move mailer inside namespace

I've got a Rails 3 mailer that works fine.

class Notifier < ActionMailer::Base
  def cool_email(user_id)
     @user = User.find_by_id(user_id)
     mail(to: @user.email,
          from: '[email protected]',            
          subject: 'hi'
       )
    end 
 end

The view for this will render the @user instance variable correctly and the email is sent without any problem.

However, when I namespace the mailer, everything breaks. With the mailer structured like this.

class Foo::Notifier < ::ActionMailer::Base
  def cool_email(user_id)
     @user = User.find_by_id(user_id)
     mail(to: @user.email,
          from: '[email protected]',            
          subject: 'hi'
       )
    end 
 end

And the view inside app/view/foo, Rails is unable to find the html template. The email sends, but there is nothing inside the body.

What am I doing wrong?

Upvotes: 2

Views: 1353

Answers (1)

Simone Carletti
Simone Carletti

Reputation: 176382

The views should be stored in app/view/foo/notifier, specifically app/view/foo/notifier/cool_email.EXTENSION.

FYI, it's always a good practice to append Mailer to the name of a mailer.

class Foo::NotifierMailer < ::ActionMailer::Base

or

class Foo::NotificationMailer < ::ActionMailer::Base

It prevents conflicts and makes possible to immediately understand the scope of the class.

Upvotes: 4

Related Questions