# Variable Scope Within a controller

**URL:** <https://www.ruby-forum.com/t/variable-scope-within-a-controller/184886>\
**Category:** Rails\
**Created:** [March 10, 2010, 12:41am UTC](https://www.ruby-forum.com/t/variable-scope-within-a-controller/184886 "2010-03-10T00:41:32Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ashwin\_V](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/8e7dd6/32.png) [@Ashwin\_V](https://www.ruby-forum.com/u/Ashwin_V)\
**Post date:** [March 10, 2010, 12:41am UTC](https://www.ruby-forum.com/t/variable-scope-within-a-controller/184886/1 "2010-03-10T00:41:32Z")

</div>

I have a controller for registering an user.  
i am not able to use the instance variable @username in “Process”. How  
do i use them ? and also is it safe to declare usernames and passwords  
as global variables.

I am pretty new to ruby on rails. any help would be great.

class RegisterController \< ApplicationController

def register  
@username = “User”  
@password = “XXXX”

end

def process

# puts “#{@username}” - Doesnt work

end

end

-Ashwin

---

<div class="post-metadata">

**Author:** ![Ashwin\_V](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/8e7dd6/32.png) [@Ashwin\_V](https://www.ruby-forum.com/u/Ashwin_V)\
**Post date:** [March 10, 2010, 12:59am UTC](https://www.ruby-forum.com/t/variable-scope-within-a-controller/184886/2 "2010-03-10T00:59:40Z")

</div>

On Mar 9, 11:40 pm, Ashwin V. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I have a controller for registering an user.  
> i am not able to use the instance variable @username in “Process”. How  
> do i use them ? and also is it safe to declare usernames and passwords  
> as global variables.

While process and register are two instance methods of your  
controller, if your user first goes to the register page and then to  
the process page then those are two difference instances of  
RegisterController. In a real world deployment they could be handled  
by different instances of mongrel/unicorn/passenger/etc, possibly by  
different servers. Global variables are also a bad idea. Your choices  
for state are pretty much the database, cookies (or the session, but  
that sits on top of either cookies or the database) or, to some extent  
the html that you spit out

Fred

---

<div class="post-metadata">

**Author:** ![Ashwin\_V](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/8e7dd6/32.png) [@Ashwin\_V](https://www.ruby-forum.com/u/Ashwin_V)\
**Post date:** [March 10, 2010, 1:13am UTC](https://www.ruby-forum.com/t/variable-scope-within-a-controller/184886/3 "2010-03-10T01:13:08Z")

</div>

I am not currently using database for my application, i am getting the  
data from forms, and calling a register api  
[register(username,pwd,email)].  
The only reason i want the username and pwd for “process” is cause  
my registration page is a 2 step process. and for each step i have  
defined an instance method.  
so the first step i register the user, and second step i add some  
details . so in the second step i would be calling a function  
[process(username,pwd,Details)]  
consider “details” to be a bunch of data in xml format. and i want the  
user to enter only details in the second page. so the second page has  
to be aware of the username and pwd values.

On Mar 9, 3:58 pm, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![Ashwin\_V](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/8e7dd6/32.png) [@Ashwin\_V](https://www.ruby-forum.com/u/Ashwin_V)\
**Post date:** [March 10, 2010, 8:02am UTC](https://www.ruby-forum.com/t/variable-scope-within-a-controller/184886/4 "2010-03-10T08:02:36Z")

</div>

Hi,

is just an idea but you can try to store this data in the flash hash,  
than kind of behavior is why flash hash exist.

Cristian Vasquez  
MedellÃ­n - Colombia
