Accessing session variables

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 {}

– -- 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.

session[:id] = 3

a = session[:id]

Zon
http://www.ZONator.com

Thanks for the reply. Neither of those seem to work. Here is my code
from the view:

<%= link_to_remote( “1”, {:url => { :controller => “ratings”, :action =>
“ratethis”, :pageid => @page.id, :rateabletype => “xyz”, :rating => 1}},
:class => ‘one-star’, :name => ‘1 star out of 5’) %>

Here is the action in the controller for adding the record to the
database. All fields are being inserted successfully except for the
userid field. As I mentioned earlier, I am storing the user ID value in
the session and simply need to use it here:

def ratethis
@rating = Rating.new(:rating => params[:rating], :pageid =>
params[:pageid], :rateabletype => params[:rateabletype], :date=>
Time.now, :userid => NEEDTHIS)

if @rating.save
flash[:notice] = ‘Rating was successfully created.’
redirect_to :action => ‘list’
else
render :action => ‘new’
end

end

This is not the a session variable, is a request variable:

I can’t try right now, but you should pass userid in the link_to_remote

, :name => ‘1 star out of 5’, userid => ‘33’

then you read it with params[:userid]

Zon
http://www.ZONator.com

Looking at the dump of the session in your first message you’ve put the
whole user object in the session,
so to get the id of that user you’d have to do session[:user].id

In general you should be careful about what you put in the session
(beyond basic stuff like hashes, arrays, strings, numbers etc…). The
main reason is that if your User class changes significantly then rails
can be unable to unserialize the session from wherever you’ve stored it,
leaving your users with a rather unhelpful error message. In this case
I’d just put the user id in the session

Fred

:userid => session[:id]

This really doesn’t work? Try adding ‘debug(session)’ to your layout to
make sure everything is right in your session variable before
submitting.

Also, if you want to simplify your creates, remove the ‘:date=>
Time.now’ and add a ‘created_at’ field to your table. Rails will
automatically fill this in when a record is added.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs