在方法中更改邮件布局
Change mailer layout in method
如何在邮件程序中的方法中更改电子邮件的整体布局?
尝试从本质上做到这一点:
class AccountMailer < ActionMailer::Base
layout 'mailer'
def reset_password(user)
layout 'simple_mailer'
end
end
但这会引发错误。
基本上我在 /app/views/layouts/simple_mailer.html.erb
中有一个我想使用的布局,但仅限于那个方法。
我是 运行 Rails 4.2.
这个 article 回答你的问题。只需在 render
方法中指定 :layout
选项。
mail
方法可以接受传入格式对象的块。
class MyMailer < ApplicationMailer
layout 'mailer'
def password_reset(user)
# uses the "mailer" layout
end
def special
mail(to: "whomever") do |format|
format.html { render(layout: false) } # no layout is used
format.text # use the special.text.erb like normal
end
end
end
您可以将其他选项传递给 mail 块并指定要使用的布局,如下所示:
class AccountMailer < ActionMailer::Base
layout 'mailer'
def reset_password(user)
mail(to: 'test@gmail.com', subject: 'Sample Mail', from: "<test@gmail.com>") do |format|
format.html { render layout: 'path_to/other_mailer_layout' }
end
end
end
如何在邮件程序中的方法中更改电子邮件的整体布局?
尝试从本质上做到这一点:
class AccountMailer < ActionMailer::Base
layout 'mailer'
def reset_password(user)
layout 'simple_mailer'
end
end
但这会引发错误。
基本上我在 /app/views/layouts/simple_mailer.html.erb
中有一个我想使用的布局,但仅限于那个方法。
我是 运行 Rails 4.2.
这个 article 回答你的问题。只需在 render
方法中指定 :layout
选项。
mail
方法可以接受传入格式对象的块。
class MyMailer < ApplicationMailer
layout 'mailer'
def password_reset(user)
# uses the "mailer" layout
end
def special
mail(to: "whomever") do |format|
format.html { render(layout: false) } # no layout is used
format.text # use the special.text.erb like normal
end
end
end
您可以将其他选项传递给 mail 块并指定要使用的布局,如下所示:
class AccountMailer < ActionMailer::Base
layout 'mailer'
def reset_password(user)
mail(to: 'test@gmail.com', subject: 'Sample Mail', from: "<test@gmail.com>") do |format|
format.html { render layout: 'path_to/other_mailer_layout' }
end
end
end