Question

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

Was it helpful?

Solution

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

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top