# Howto set a global parameter

**URL:** <https://www.ruby-forum.com/t/howto-set-a-global-parameter/57142>\
**Category:** Rails\
**Created:** [April 7, 2006, 2:21pm UTC](https://www.ruby-forum.com/t/howto-set-a-global-parameter/57142 "2006-04-07T14:21:42Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![msojka](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/b5a626/32.png) [@msojka](https://www.ruby-forum.com/u/msojka)\
**Post date:** [April 7, 2006, 2:21pm UTC](https://www.ruby-forum.com/t/howto-set-a-global-parameter/57142/1 "2006-04-07T14:21:42Z")

</div>

Hi,

In my login\_controller I get the usename of the person logging in.  
@username = @user.name  
puts " DEBUG: #{@username} just logged in\n" # This works

But @username seems empty when accessed from numbers\_controller.rb ,  
how to I set it to be accessible global?  
Or at least move the content of that variable over to another  
controller.

Best regards,  
Martin S.

---

<div class="post-metadata">

**Author:** ![msojka](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/b5a626/32.png) [@msojka](https://www.ruby-forum.com/u/msojka)\
**Post date:** [April 7, 2006, 2:46pm UTC](https://www.ruby-forum.com/t/howto-set-a-global-parameter/57142/2 "2006-04-07T14:46:00Z")

</div>

Le Vendredi 7 Avril 2006 14:18, Martin S. a Ã©critÂ :

> Hi,

Hi

> In my login\_controller I get the usename of the person logging in.  
> @username = @user.name  
> puts " DEBUG: #{@username} just logged in\n" # This works
> 
> But @username seems empty when accessed from numbers\_controller.rb ,  
> how to I set it to be accessible global?  
> Or at least move the content of that variable over to another  
> controller.  
> This is perfectly normal, as @username is an instance variable of your  
> login  
> controller and so does not belong to any other controller.

But what you need is a session, I think :  
session[:username] = @user.name

And you’ll be able to access it from any controller.

Cheers,

---

<div class="post-metadata">

**Author:** ![msojka](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/b5a626/32.png) [@msojka](https://www.ruby-forum.com/u/msojka)\
**Post date:** [April 11, 2006, 3:08am UTC](https://www.ruby-forum.com/t/howto-set-a-global-parameter/57142/3 "2006-04-11T03:08:26Z")

</div>

I’m assuming by global that you want a variable to exist in all  
controllers and views. While using sessions to accomplish this works,  
one ends up having duplicate code to access these sessions, resulting  
in a coupled unmanageable controller/view set. I found that the best  
solution for “globals” is to define the applicationController’s  
initialize method and create the “global” data as an instance  
variable. For example, to share a user model across all views and  
controllers code the following:

class ApplicationController \< ActionController::Base  
model :user  
model :permission

def initialize  
@user = session[:user] ||= User.find\_by\_username(‘nobody’, :include  
=\> ‘permissions’)  
end

end

where session[:user] exists or the default ‘nobody’ user is created  
and assigned to both @user and session[:user].

so far I have found nothing wrong with defining the initializer.

- travis

I am not responsible for any “damages” resulting from the advice that  
I give. I reserve the right to change my mind and be victim to any  
shift in circumstances.

---

<div class="post-metadata">

**Author:** ![msojka](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/b5a626/32.png) [@msojka](https://www.ruby-forum.com/u/msojka)\
**Post date:** [April 11, 2006, 3:20am UTC](https://www.ruby-forum.com/t/howto-set-a-global-parameter/57142/4 "2006-04-11T03:20:38Z")

</div>

plus, there is also a good post called “Sessions or Lookups” back in  
the middle of march. Furthermore, I just tried the code I wrote \>\<  
apparently sessions are not loaded at the time the  
ApplicationController is initialized.
