views:

57

answers:

2

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

+2  A: 

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

nas
A: 

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.

Thorbjørn Hermansen