PBJ
PBJ

Reputation: 767

Set subject from ActionMailer template in Rails 3?

How do I set the subject from a Rails 3 ActionMailer template?

I'd like to keep the subject and body together.

In Rails 2 you can do the following:

<% controller.subject = 'Change of subject' %>

(from http://www.quirkey.com/blog/2008/08/29/actionmailer-changing-the-subject-in-the-template/)

Upvotes: 8

Views: 4792

Answers (2)

chrisgooley
chrisgooley

Reputation: 774

To set the subject line of an email from the email view itself you can simply put the following at the beginning of the view file:

<% message.subject = 'This is my subject line' %>

This works for rails 3 and 4.

Upvotes: 13

corroded
corroded

Reputation: 21564

http://edgeguides.rubyonrails.org/action_mailer_basics.html

it says here that:

class UserMailer < ActionMailer::Base
default :from => "[email protected]"

def welcome_email(user)
  @user = user
  @url  = "http://example.com/login"
  mail(:to => user.email, :subject => "Welcome to My Awesome Site")
  end
end

If you want to access it from the view:

http://apidock.com/rails/ActionMailer/Base

If you need to access the subject, from or the recipients in the view, you can do that through message object:

 You got a new note from <%= message.from %>!
 <%= truncate(@note.body, 25) %>

So you can do:

message.subject

Upvotes: 18

Related Questions