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