# Scope of Instance Variables with Partial Views

**URL:** <https://www.ruby-forum.com/t/scope-of-instance-variables-with-partial-views/161732>\
**Category:** Rails\
**Created:** [March 1, 2009, 9:48pm UTC](https://www.ruby-forum.com/t/scope-of-instance-variables-with-partial-views/161732 "2009-03-01T21:48:46Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![rube\_noob](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/bbe5ce/32.png) [@rube\_noob](https://www.ruby-forum.com/u/rube_noob)\
**Post date:** [March 1, 2009, 9:48pm UTC](https://www.ruby-forum.com/t/scope-of-instance-variables-with-partial-views/161732/1 "2009-03-01T21:48:46Z")

</div>

Hello

I do not understand how to use instance variable properly with partial  
views, I am hoping someone here can enlighten me. For example

class MainController \< ApplicationController  
def index  
@item\_list = Item.find\_all\_item  
end

```
def detail_display
    @current_selected = @item= Item.find(params[:id])
    redirect_to :action => :index
end

```

end

detail\_display is invoked when the user clicks on an item in the list.  
The variable @current\_selected is not available to the partial view  
invoked when the index is redirected to. How can I remedy this?

Thank you

rube\_noob

---

<div class="post-metadata">

**Author:** ![rube\_noob](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/bbe5ce/32.png) [@rube\_noob](https://www.ruby-forum.com/u/rube_noob)\
**Post date:** [March 1, 2009, 10:01pm UTC](https://www.ruby-forum.com/t/scope-of-instance-variables-with-partial-views/161732/2 "2009-03-01T22:01:03Z")

</div>

On Mar 1, 8:48 pm, Rube N. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ```
> def detail_display
> @current_selected = @item= Item.find(params[:id])
> redirect_to :action => :index
> end
> 
> ```
> 
> end
> 
> detail\_display is invoked when the user clicks on an item in the list.  
> The variable @current\_selected is not available to the partial view  
> invoked when the index is redirected to. How can I remedy this?

The key thing you need to understand here is that this is an http  
redirect, ie a separate request: by the time the index action is  
rendered the controller object that had the @current\_selected instance  
variable has been destroyed and your request is being handled by a  
completely fresh controller object. If the index action needs to know  
what the currently selected object is then you need to pass a  
parameter to it telling it this. In this light, I’m not sure what the  
point of the detail\_display action is as it seems to be a complete no-  
op.

Fred

---

<div class="post-metadata">

**Author:** ![rube\_noob](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/bbe5ce/32.png) [@rube\_noob](https://www.ruby-forum.com/u/rube_noob)\
**Post date:** [March 1, 2009, 10:12pm UTC](https://www.ruby-forum.com/t/scope-of-instance-variables-with-partial-views/161732/3 "2009-03-01T22:12:40Z")

</div>

Hi Fred,

Thanks for your response,

Basically what I am trying to accomplish is have to partial views in the  
index page. One for the list of items and one for the details of the  
selected item. In the future I hope to add new functionality to the  
index page and I hope that the index method will not have to deal with  
all the different actions that I add.

I understand how this is a new HTTP request and that the  
@current\_selected variable is lost. Is there any way I can allow the  
view to see this variable without involving the index method?

rube\_noob

Frederick C. wrote:

> On Mar 1, 8:48ï¿½pm, Rube N. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > ï¿½ ï¿½ def detail\_display  
> > ï¿½ ï¿½ ï¿½ ï¿½ @current\_selected = @item= Item.find(params[:id])  
> > ï¿½ ï¿½ ï¿½ ï¿½ redirect\_to :action =\> :index  
> > ï¿½ ï¿½ end  
> > end
> > 
> > detail\_display is invoked when the user clicks on an item in the list.  
> > The variable @current\_selected is not available to the partial view  
> > invoked when the index is redirected to. How can I remedy this?
> 
> The key thing you need to understand here is that this is an http  
> redirect, ie a separate request: by the time the index action is  
> rendered the controller object that had the @current\_selected instance  
> variable has been destroyed and your request is being handled by a  
> completely fresh controller object. If the index action needs to know  
> what the currently selected object is then you need to pass a  
> parameter to it telling it this. In this light, I’m not sure what the  
> point of the detail\_display action is as it seems to be a complete no-  
> op.
> 
> Fred

---

<div class="post-metadata">

**Author:** ![rube\_noob](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/bbe5ce/32.png) [@rube\_noob](https://www.ruby-forum.com/u/rube_noob)\
**Post date:** [March 1, 2009, 10:22pm UTC](https://www.ruby-forum.com/t/scope-of-instance-variables-with-partial-views/161732/4 "2009-03-01T22:22:18Z")

</div>

On Mar 1, 9:12 pm, Rube N. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I understand how this is a new HTTP request and that the  
> @current\_selected variable is lost. Is there any way I can allow the  
> view to see this variable without involving the index method?

Nope, you’d have to pass a parameter to the index action (although  
whether manipulation of that parameter happens in the index action  
itself or in a before\_filter is up to you)

Fred
