From f1d77bad265bd3f8f2c621c69613909e46e9e6ea Mon Sep 17 00:00:00 2001 From: Alberto Perdomo Date: Thu, 23 Jun 2011 17:57:48 +0100 Subject: Action Mailer Basics Guide: Added example on using mail(:template_path => ..., :template_name => ...) to specify custom template files. --- .../guides/source/action_mailer_basics.textile | 25 ++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) (limited to 'railties') diff --git a/railties/guides/source/action_mailer_basics.textile b/railties/guides/source/action_mailer_basics.textile index e1ff49cd60..2eaee158ff 100644 --- a/railties/guides/source/action_mailer_basics.textile +++ b/railties/guides/source/action_mailer_basics.textile @@ -276,6 +276,27 @@ Mailer views are located in the +app/views/name_of_mailer_class+ directory. The To change the default mailer view for your action you do something like: + +class UserMailer < ActionMailer::Base + default :from => "notifications@example.com" + + def welcome_email(user) + @user = user + @url = "http://example.com/login" + mail(:to => user.email, + :subject => "Welcome to My Awesome Site", + :template_path => 'notifications', + :template_name => 'another') + end + end + +end + + +In this case it will look for templates at +app/views/notifications+ with name +another+. + +If you want more flexibility you can also pass a block and render specific templates or even render inline or text without using a template file: + class UserMailer < ActionMailer::Base default :from => "notifications@example.com" @@ -286,14 +307,14 @@ class UserMailer < ActionMailer::Base mail(:to => user.email, :subject => "Welcome to My Awesome Site") do |format| format.html { render 'another_template' } - format.text { render 'another_template' } + format.text { render :text => 'Render text' } end end end -Will render 'another_template.text.erb' and 'another_template.html.erb'. The render command is the same one used inside of Action Controller, so you can use all the same options, such as :text etc. +This will render the template 'another_template.html.erb' for the HTML part and use the rendered text for the text part. The render command is the same one used inside of Action Controller, so you can use all the same options, such as :text, :inline etc. h4. Action Mailer Layouts -- cgit v1.2.3