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:
<% control开发者_运维问答ler.subject = 'Change of subject' %>
(from http://www.quirkey.com/blog/2008/08/29/actionmailer-changing-the-subject-in-the-template/)
http://edgeguides.rubyonrails.org/action_mailer_basics.html
it says here that:
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")
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
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.
精彩评论