質問

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: 'admin@example.com',            
          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: 'admin@example.com',            
          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?

役に立ちましたか?

解決

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.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top