如何在Actionmailer消息中使用不同的布局?

时间:2013-02-25 23:40:47

标签: ruby-on-rails actionmailer

我有带布局的project_mailer但如果project_notification方法的参数为unsubscribe_link = true,我想使用不同的方法。

layout "project_mail"

def project_notification(user, projects, unsubsribe_link = false)
  attachments.inline['logo_252.png'] = File.read(Rails.root + 'public/images/logo_252.png')
  @user = user
  @projects = projects

  mail(:to => user.email, :subject => "New Projects")
end

1 个答案:

答案 0 :(得分:16)

我认为你已经解决了你的问题,我回答帮助别人:

layout 'project_mail'

def project_notification(user, projects, unsubscribe_link = false)
  attachments.inline['logo_252.png'] = File.read(Rails.root + 'public/images/logo_252.png')
  @user = user
  @projects = projects
  layout_name = unsubscribe_link ? 'other_fancy_layout' : 'project_mail'

  mail(to: user.email, subject: "New Projects") do |format|
    format.html { render layout: layout_name }
    format.text { render layout: layout_name }
  end
end