# Passing instance variable between controllers

**URL:** <https://www.ruby-forum.com/t/passing-instance-variable-between-controllers/124575>\
**Category:** Rails\
**Created:** [December 30, 2007, 2:33pm UTC](https://www.ruby-forum.com/t/passing-instance-variable-between-controllers/124575 "2007-12-30T14:33:07Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![ankit2584](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/51bf81/32.png) [@ankit2584](https://www.ruby-forum.com/u/ankit2584)\
**Post date:** [December 30, 2007, 2:33pm UTC](https://www.ruby-forum.com/t/passing-instance-variable-between-controllers/124575/1 "2007-12-30T14:33:07Z")

</div>

Hello,  
I have two controllers mailinglist\_controller and  
mailtemplate\_controller  
The mailinglist controller has an instance variable @mailinglist.

how can I to use the instance variable @mailinglists from controller  
mailinglist\_controller in mailtemplate\_controller.?

The control goes as follows

[http://localhost:3000/mailinglist/mailinglists](http://localhost:3000/mailinglist/mailinglists)  
This displays the lists. The user selects the lists by ticking  
checkboxes  
and then clicks the “write mail” button

[http://localhost:3000/mailinglist/getmailinglists](http://localhost:3000/mailinglist/getmailinglists)  
The function getmailinglists in the mailinglist controller gets all the  
mailinglist selected by the user and stores them in @mailinglists  
instance variable.The last line in the getmailinglists function is

render :action=\>“write\_mail”

This brings the user to the page where there is a text box wherethe user  
can type in the mail content.

after the user clicks on “send mail” button  
the control goes to  
[http://localhost:3000/mailtemplate/sendmail](http://localhost:3000/mailtemplate/sendmail)

I need to access the @mailinglists values that i had before in the  
mailinglist controller. How can i do that. The sendmail function is  
below…The @mailinglists returns nil at present because the values are  
not retained from the previous controller.

def sendmail  
@mailinglists.each do |ml1|

@people =  
Person.find(:all,:include=\>:mailinglists,:conditions=\>[‘mailinglist\_id =  
?’,ml1.id])  
@people.each do|person|  
UserMailer.deliver\_send\_mail(person,params[:textarea])  
end  
end  
render :action=\>“…/mailinglist/send\_mail”  
return  
end

---

<div class="post-metadata">

**Author:** ![ankit2584](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/51bf81/32.png) [@ankit2584](https://www.ruby-forum.com/u/ankit2584)\
**Post date:** [December 31, 2007, 6:33pm UTC](https://www.ruby-forum.com/t/passing-instance-variable-between-controllers/124575/2 "2007-12-31T18:33:32Z")

</div>

On 12/30/07, Ank Ag [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hello,  
> I have two controllers mailinglist\_controller and  
> mailtemplate\_controller  
> The mailinglist controller has an instance variable @mailinglist.
> 
> how can I to use the instance variable @mailinglists from controller  
> mailinglist\_controller in mailtemplate\_controller.?

You don’t, normally. An instance variable is not persistent across  
requests. You can store it in a session to make it persistent or you  
can pass it in the urls.

You can add a finder that brings the instance variable into existence,  
then share that finder method with both controllers by placing it in  
app/controllers/application.rb.

–  
Greg D.  
[http://destiney.com/](http://destiney.com/)
