Forum: Ruby on Rails Howto set a global parameter

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
3b1daae9f655c5dba51ff1cdf03f1bef?d=identicon&s=25 Martin Stabenfeldt (Guest)
on 2006-04-07 14:21
(Received via mailing list)
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 Stabenfeldt
5892e4bc15f96968740e8a6f70ccc88a?d=identicon&s=25 Loïc Guitaut (Guest)
on 2006-04-07 14:46
(Received via mailing list)
Le Vendredi 7 Avril 2006 14:18, Martin Stabenfeldt 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,
1c357f90ccd53e1d0e12e9dd08a4940c?d=identicon&s=25 travis michel (Guest)
on 2006-04-11 03:08
(Received via mailing list)
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.
1c357f90ccd53e1d0e12e9dd08a4940c?d=identicon&s=25 travis michel (Guest)
on 2006-04-11 03:20
(Received via mailing list)
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.
This topic is locked and can not be replied to.