Re: Accessing session variables

Cayce Balara
2006-11-15T02:57:30Z

-- -- wrote:
> I need to reference the user ID variable in a view and I am uncertain of
> the proper syntax. The user ID is stored in a session variable. What is
> the proper syntax to use?
>
> (Also, are there any useful references for ruby syntax online? I
can''t
> seem to find any good ones.)
>
> For reference, here is a dump of the session info:
>
> --- !ruby/object:CGI::Session
> data: &id001
>   user: !ruby/object:User
>     attributes:
>       id: "1"
>       password: c90121c2fbb54ec2133fb0c868dc1b04e354fe66
>       login: jsmith
>   flash: !map:ActionController::Flash::FlashHash {}

Session variables are accessed in the session hash - as in
session[''var'']. If your session variable is
''user'', then in a view you
would access it as such:

<%= session[''user''] %>

The old version of the pickaxe ruby book is available at
http://www.rubycentral.com/book/index.html but sooner or later you''ll
want the latest edition. Then there''s Why''s Poignant Guide to
Ruby at
http://poignantguide.net/ruby/.

c.