ActionMailer将局部变量传递到erb模板 [英] ActionMailer pass local variables to the erb template

查看:66
本文介绍了ActionMailer将局部变量传递到erb模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道我可以定义实例变量,例如:

I know I could define instance variables e.g:

def user_register(username, email)
  @username = username
  @email = email

  mail(:to => email, :subject => "Welcome!", :template_name => "reg_#{I18n.locale}")
end

但是,有没有办法使用 local 变量,就像将:locals 传递给partials一样?

But, is there a way to use local variables instead, just like passing :locals to partials?

推荐答案

mail方法中可用的所有选项都可以在

All options available in the mail method can be found at http://api.rubyonrails.org/classes/ActionMailer/Base.html#method-i-mail.

我们知道render具有:locals选项.但是,我们可以看到mail没有可用的:locals选项.因此,没有,没有比使用实例变量更好的方法了(除非您想使用诸如全局变量或持久数据库对象之类的丑陋的东西-不要这样做).

We know that render has a :locals option. However we can see that there is no :locals option available for mail. Therefore, no, there is no better way than to use instance variables (unless you want to use something hideous like globals or persistent database objects - don't do this).

实例变量就是您要使用的.

Instance variables are what you are meant to use.

这篇关于ActionMailer将局部变量传递到erb模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆