在Ruby on Rails中如何在Mailer中使用实例变量? [英] How do you use an instance variable with mailer in Ruby on Rails?
本文介绍了在Ruby on Rails中如何在Mailer中使用实例变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在邮件程序模型中创建了一个实例变量(@user),并想在视图中访问它?但这给我一个错误(@user = nil)。传递变量以查看(电子邮件正文)的最佳方法是什么?
I created an instance variable (@user) in the model of a mailer and want to access it in the view? But it's giving me an an error (@user = nil). What's the best way to pass a variable to view (the email body)?
感谢Chirag
推荐答案
如果要访问邮件模板中的实例变量,请在邮件模型中添加
If you want to access an instance variable in your mailer template then in your mailer model add
@body[:user] = user_object
上面将创建一个实例变量@user,可以是在您的视图中访问。
The above would create an instance variable @user that can be accessed in your view. You should be able to access user object in your mailer view by doing
@user
文档如果要使用多种格式(文本/ html),请参见此处的示例。
这篇关于在Ruby on Rails中如何在Mailer中使用实例变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文