What's New in Edge Rails: Mailer Layouts 11

Posted by ryan
at 5:10 PM on Sunday, September 07, 2008

Rails mailer templates now have the ability to utilize layouts just like their view-template siblings. The only caveat is that for a mailer layout to be automatically recognized it must end with _mailer. So this mailer:

1
2
3
4
5
6
class UserMailer < ActionMailer::Base
  def registration(user)
    subject    "You've registered"
    from       "system@example.com"
  end
end

would need a layout at layouts/user_mailer.html.erb. If you explicitly specify the layout it doesn’t need the _mailer suffix. So, in this example, a layout at layouts/email.html.erb would suffice:

1
2
3
4
class UserMailer < ActionMailer::Base
  layout 'email'
  ...
end

Go forth and make your emails pretty. You’ve got all the tools now…

tags: ruby, rubyonrails

Comments

Leave a response

  1. Ben JohnsonSeptember 07, 2008 @ 05:38 PM

    Awesome! I’ve been doing this with a plugin I wrote, but I’m glad it’s finally added in. This is something that should have been added in a long time ago.

  2. CarstenSeptember 07, 2008 @ 05:51 PM

    Yay!

  3. Humble Rails DeveloperSeptember 08, 2008 @ 12:33 AM

    Ridiculous. This feature is like two years overdue.

  4. CarlSeptember 08, 2008 @ 01:13 PM

    I was wondering why I couldn’t find a way to set the layout before. Now I know why. Of course, now I wonder why it didn’t exist before?

  5. BankSeptember 11, 2008 @ 02:56 AM

    Usefull post! I will use it in my next project.

  6. RahsunSeptember 11, 2008 @ 10:53 AM

    Yay! Very useful, I’m so glad they added this. No more hacking around to make it happen.

  7. Scott M.September 12, 2008 @ 05:34 PM

    In other news, ActionMailer still sucks ;-)

  8. Jerry LogginsSeptember 15, 2008 @ 06:22 AM

    I’m so glad to hear they’ve finally done this! Thanks for the post!

  9. PratikSeptember 21, 2008 @ 03:11 PM

    Hey Scott,

    Why not help instead of just leaving comments ? Least you could do is make an actual wishlist of things you would like to see changed/implemented in AM. You never know, it might actually happen ;-)

  10. Kendall SueOctober 01, 2008 @ 09:02 AM

    I finally found the help I have been searching for. Thank you so much for writing this!!! But more than that, thanks a bunch for the help!!

    Kendall

  11. DwayneNovember 06, 2008 @ 09:40 PM

    I’m getting

    undefined method `layout’ for KMailer:Class /usr/lib/ruby/gems/1.8/gems/actionmailer-2.1.1/lib/action_mailer/base.rb:385:in `method_missing’

    tips?

Comment