ActionMailer将局部变量传递到erb模板 [英] ActionMailer pass local variables to the erb template
问题描述
我知道我可以定义实例变量,例如:
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?
推荐答案
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屋!