开发者

How do you use an instance variable with mailer in Ruby on Rails?

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)?

Thanks, Chirag


If you want to access an instance variable in your mailer template then in your mailer model add

@body[:user] = user_object

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

The docs here have examples of alternative ways if you are using multiple formats (text/html).


I Rails 3 the process is similar:

@user = user_object

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

Note that you need to set this variable before the

mail(:from => "info@domain.info", :to => recipient, :subject => "Subject")


To pass a variable to the view/email body, you send them in via the body method :-) So, for example, body :account => recipient would result in an instance variable @account with the value of recipient being accessible in the view.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜