Render Different View (template) for ActionMailer

后端 未结 3 1092
再見小時候
再見小時候 2021-02-03 19:39

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 s

相关标签:
3条回答
  • 2021-02-03 19:49

    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)
    
    0 讨论(0)
  • 2021-02-03 20:05

    The solution from Sean Hill doesn't work for me (Rails 3.2+). template_name seems to be ignored. What worked for me is something like this:

    mail(:to => "#{@user.name} <#{@user.email}>", :subject => "Welcome to ...") do |format|
      format.html { render 'templatename' }
    end
    
    0 讨论(0)
  • 2021-02-03 20:10

    Funny in rails 3.2.14 This does NOT work for me:

    mail(:to => "#{@user.name} <#{@user.email}>", :subject => "Welcome to ...") do |format|
      format.html { render 'templatename' }
    end
    

    However this does:

    mail(:to => "#{@user.name} <#{@user.email}>", 
     :subject => "Welcome to ...", 
    :template_name => template)
    
    0 讨论(0)
提交回复
热议问题