# Controller member variables

**URL:** <https://www.ruby-forum.com/t/controller-member-variables/108669>\
**Category:** Rails\
**Created:** [July 5, 2007, 9:33pm UTC](https://www.ruby-forum.com/t/controller-member-variables/108669 "2007-07-05T21:33:08Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![reacher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/0ea827/32.png) [@reacher](https://www.ruby-forum.com/u/reacher)\
**Post date:** [July 5, 2007, 9:33pm UTC](https://www.ruby-forum.com/t/controller-member-variables/108669/1 "2007-07-05T21:33:08Z")

</div>

I’m failing to see why a member variable in my controller won’t keep  
it’s value. Here’s the setup:

editor\_controller.rb:

…  
def index  
display\_loops  
end

def mytoggle  
@show\_loops = !@show\_loops  
redirect\_to :action =\> “index”  
end  
…

index.rhtml:

### \<% if @show\_loops %\> \<% link\_to "Hide Loops", { :action =\> "mytoggle" } %\> \<% else %\> \<% link\_to "Show Loops", { :action =\> "mytoggle" } %\> \<% end %\>
  
...

When this HTML is rendered and i click on “Hide Loops”, the  
@show\_loops variable does get toggled from false to true, but when it  
gets redirected back to index, the variable is back to false. Am I  
missing something obvious?

---

<div class="post-metadata">

**Author:** ![reacher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/0ea827/32.png) [@reacher](https://www.ruby-forum.com/u/reacher)\
**Post date:** [July 5, 2007, 9:48pm UTC](https://www.ruby-forum.com/t/controller-member-variables/108669/2 "2007-07-05T21:48:13Z")

</div>

Rails controllers are not kept around. When a request comes in, a new  
instance of the controller is made for that request only. Controller  
instance variables are mainly used for flagging what data is to be  
available  
in the view. If you need data to persist across requests, you’ll need to  
put  
it in the session.

Jason

---

<div class="post-metadata">

**Author:** ![reacher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/0ea827/32.png) [@reacher](https://www.ruby-forum.com/u/reacher)\
**Post date:** [July 5, 2007, 10:02pm UTC](https://www.ruby-forum.com/t/controller-member-variables/108669/3 "2007-07-05T22:02:14Z")

</div>

got it. thanks!
