为ActionMailer渲染不同的视图

为ActionMailer渲染不同的视图

本文介绍了为ActionMailer渲染不同的视图(模板)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试有条件地渲染不同于ActionMailer(Rails 3.1.1)的模板。我希望大多数用户都能获得正常的 welcome.html.erb 模板,但有些用户却能获得特殊的 welcome_photographer.html.erb 模板。这种类型的东西在ActionController中可以工作:

I'm trying to do a conditional render of a different template from ActionMailer (Rails 3.1.1). I want most users to get the normal welcome.html.erb template, but some users to get the special welcome_photographer.html.erb template. This type of thing works in ActionController:

# (in /app/mailers/user_mailer.rb)
def welcome(user)
  @user = user
  mail(:to => "#{@user.name} <#{@user.email}>", :subject => "Welcome to ...")
  render "welcome_photographer" if @user.is_photographer
end



则渲染 welcome_photographer

但是渲染无法正常工作-每个人都会获得标准的 welcome.html.erb ,即使 @ user.is_photographer == true

推荐答案

在致电 mail( )。但是,要选择其他模板,则应传递:template_name 作为选项。例如:

You shouldn't try to do anything after you call mail(). However, to choose another template, you should pass :template_name as an option. For example:

template = @user.is_photographer ? "welcome_photographer" : "welcome"
mail(:to => "#{@user.name} <#{@user.email}>",
     :subject => "Welcome to ...",
     :template_name => template)

这篇关于为ActionMailer渲染不同的视图(模板)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-01 23:06