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
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, -- ,= ,-_-. =. Lo?c Guitaut ((_/)o o(\_)) http://www.belfalas.org `-''(. .)`-'' Jabber ID : Flink@im.apinc.org \_/ GnuPG KeyID : 0xA78CD85D -------------- next part -------------- A non-text attachment was scrubbed... Name: not available Type: application/pgp-signature Size: 189 bytes Desc: not available Url : http://wrath.rubyonrails.org/pipermail/rails/attachments/20060407/d6d46b42/attachment.bin
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.
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.