从Rails 3中的ActionMailer模板设置主题?

PBJ*_*PBJ 8 email ruby-on-rails actionmailer

如何从Rails 3 ActionMailer模板设置主题?

我想把主题和身体放在一起.

在Rails 2中,您可以执行以下操作:

<% controller.subject = 'Change of subject' %>
Run Code Online (Sandbox Code Playgroud)

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

cor*_*ded 17

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

它在这里说:

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
Run Code Online (Sandbox Code Playgroud)

如果要从视图中访问它:

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

如果您需要访问主题,来自视图中的收件人,您可以通过消息对象访问:

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

所以你可以这样做:

message.subject
Run Code Online (Sandbox Code Playgroud)

  • 不,我想从模板*而不是在邮件代码中设置主题*. (3认同)

chr*_*ley 11

要从电子邮件视图本身设置电子邮件的主题行,您只需将以下内容放在视图文件的开头:

<% message.subject = 'This is my subject line' %>
Run Code Online (Sandbox Code Playgroud)

这适用于导轨3和4.