# Lame newbie question: how to pass instance variables

**URL:** <https://www.ruby-forum.com/t/lame-newbie-question-how-to-pass-instance-variables/54694>\
**Category:** Rails\
**Created:** [March 13, 2006, 10:38am UTC](https://www.ruby-forum.com/t/lame-newbie-question-how-to-pass-instance-variables/54694 "2006-03-13T10:38:10Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![rhubarb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/59ef9b/32.png) [@rhubarb](https://www.ruby-forum.com/u/rhubarb)\
**Post date:** [March 13, 2006, 10:38am UTC](https://www.ruby-forum.com/t/lame-newbie-question-how-to-pass-instance-variables/54694/1 "2006-03-13T10:38:10Z")

</div>

This should be obvious - but I’m a long-time non-web app programmer and  
new to rails.

I simply want to put something in a variable in one action and access it  
in another.

It’s like this, I’m doing a simple photo gallery/managment app, and I  
have two actions in my picture\_controller:

The first one scans the picture directory and creates a new object in  
the pictures table for each picture it finds. The work is all done in a  
method in the Picture model which returns an array containing the new  
picture objects.

The second method is a normal list method, but instead of getting it’s  
data from the db, gets it from that scan method - because it only lists  
newly found pictures. At least that’s the plan.  
How do I pass the array found in the first action to the second one.

So in the action we have something like this:

def scan\_directory  
# new\_pics is an instance variable I’m creating here - I want  
# to access it in my other action to get only the new pictures.  
# There’s nothing in the database record to indicate what’s new,  
it’s  
# just a question of which new pics I found when calling  
scan\_directory  
@new\_pics = Picture.load\_directory(".")  
flash[:notice] = “Original image directory was refreshed  
#{@new\_pics.length} new images found”  
redirect\_to :controller =\> ‘picture’, :action =\> ‘list\_new\_pictures’  
end

def list\_new\_pictures

```
# Is this how I should use a paginator when my list is not from the

```

db?  
# Note that I get an exception here, because @new\_pics is always  
nil!

```
@picture_pages = Paginator.new self, @new_pics.count, 12,

```

@params[‘page’]

```
@pictures = @new_pics
render :action => 'list'

```

end

So what am I doing wrong? Is it not right to expect that instance  
variables to hold values across actions? Why not?

Should I be shoving the @new\_pics array into one of the hashes that gets  
passed back and forth between the view and controller?

Is my paginator correct?

any help appreciated

---

<div class="post-metadata">

**Author:** ![rhubarb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/59ef9b/32.png) [@rhubarb](https://www.ruby-forum.com/u/rhubarb)\
**Post date:** [March 13, 2006, 11:28am UTC](https://www.ruby-forum.com/t/lame-newbie-question-how-to-pass-instance-variables/54694/2 "2006-03-13T11:28:25Z")

</div>

bump - simple question - someone must know.

---

<div class="post-metadata">

**Author:** ![rhubarb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/59ef9b/32.png) [@rhubarb](https://www.ruby-forum.com/u/rhubarb)\
**Post date:** [March 13, 2006, 12:37pm UTC](https://www.ruby-forum.com/t/lame-newbie-question-how-to-pass-instance-variables/54694/3 "2006-03-13T12:37:49Z")

</div>

rhubarb wrote:

> So what am I doing wrong? Is it not right to expect that instance  
> variables to hold values across actions? Why not?  
> They don’t hold their values across actions because Rails’ execution  
> model dictates that each action takes place in a new instance of the  
> controller.

There are two ways to get what you’re trying to do to work. The first  
is to call render rather than redirect\_to in the scan\_directory action.  
That would call the list\_new\_pictures method directly in the same  
controller, so the instance variable would be available. The second  
would be to put your list of pictures into either the session or the  
params hash before issuing the redirect, then fishing it out again in  
the list\_new\_pictures action.

I’d tend towards the former, because it’s less code.

I can’t comment on the paginator, because I don’t use it much…

---

<div class="post-metadata">

**Author:** ![rhubarb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/59ef9b/32.png) [@rhubarb](https://www.ruby-forum.com/u/rhubarb)\
**Post date:** [March 13, 2006, 8:27pm UTC](https://www.ruby-forum.com/t/lame-newbie-question-how-to-pass-instance-variables/54694/4 "2006-03-13T20:27:46Z")

</div>

you can put it in session, params or flash (the last one was missing).  
flash  
is cleared after a page rendering, and this is why I use it rather than  
the  
others.

---

<div class="post-metadata">

**Author:** ![rhubarb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/59ef9b/32.png) [@rhubarb](https://www.ruby-forum.com/u/rhubarb)\
**Post date:** [March 13, 2006, 9:50pm UTC](https://www.ruby-forum.com/t/lame-newbie-question-how-to-pass-instance-variables/54694/5 "2006-03-13T21:50:54Z")

</div>

Nicolas B. wrote:

> you can put it in session, params or flash (the last one was missing).  
> flash  
> is cleared after a page rendering, and this is why I use it rather than  
> the  
> others.

actually I’ve decided against render because I want to change the URL.  
So I’m left with one of these.  
so how do I put it in, say, the params. I tried  
@params[:new\_pictures] = new\_pics

and later in a different action

new\_pics = @params[:new\_pictures]

but I get a nil error.  
What am I doing wrong.

Also that Pagination trick I tried above isnt working - Im getting  
everything in one page. Ideas?

---

<div class="post-metadata">

**Author:** ![rhubarb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/59ef9b/32.png) [@rhubarb](https://www.ruby-forum.com/u/rhubarb)\
**Post date:** [March 13, 2006, 4:16pm UTC](https://www.ruby-forum.com/t/lame-newbie-question-how-to-pass-instance-variables/54694/6 "2006-03-13T16:16:46Z")

</div>

Alex Y. wrote:

> There are two ways to get what you’re trying to do to work. The first  
> is to call render rather than redirect\_to in the scan\_directory action.  
> That would call the list\_new\_pictures method directly in the same  
> controller, so the instance variable would be available. The second  
> would be to put your list of pictures into either the session or the  
> params hash before issuing the redirect, then fishing it out again in  
> the list\_new\_pictures action.
> 
> I’d tend towards the former, because it’s less code.
> 
> I can’t comment on the paginator, because I don’t use it much…

That’s exactly what I needed to know. That controller is reinstantiated  
for every action call is a key point that I never noticed in any of the  
docs.  
Thanks Alex!

---

<div class="post-metadata">

**Author:** ![rhubarb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/59ef9b/32.png) [@rhubarb](https://www.ruby-forum.com/u/rhubarb)\
**Post date:** [March 13, 2006, 11:01pm UTC](https://www.ruby-forum.com/t/lame-newbie-question-how-to-pass-instance-variables/54694/7 "2006-03-13T23:01:02Z")

</div>

On Mar 13, 2006, at 12:50 PM, rhubarb wrote:

> So I’m left with one of these.  
> Also that Pagination trick I tried above isnt working - Im getting  
> everything in one page. Ideas?
> 
> –

Lets break this down a bit for you. Lets assume you want to redirect  
from action foo to action bar and keep something in the params along  
the way:

def foo  
# the rest of your code  
params[:new\_pictures] = new\_pics  
redirect\_to :action =\> 'bar, :params =\> params  
end

def bar  
# now params[:new\_pictures] has what you want it to have.  
end

-Cheers  
-Ezra Z.  
Yakima Herald-Republic  
WebMaster

> **[yakimaherald.com | Yakima, Washington - We Tell Your Stories](https://www.yakimaherald.com/)**

509-577-7732  
removed\_email\_address@domain.invalid
