Forum: Ruby on Rails session usage

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.
C1e5a9e9344b6d31b9df7303e6dc378a?d=identicon&s=25 Craig White (Guest)
on 2006-02-16 01:34
(Received via mailing list)
Clearly I have the session and can see that session[:user_id] = 7

Now I want to use the results of this...in a page...so I am trying to
use the instance variable

@sess_user

and at the top of the application controller, I have either...

@sess_user = User.find([:first, "id = ?", session[:user_id])
or
@sess_user = User.find([:first, "id = ?", session[:user_id])

gives me an error...symbol as Array index

Clues?

Craig
Ac1da5d66d51a1c3b843221032247b30?d=identicon&s=25 Eric Goodwin (Guest)
on 2006-02-16 01:37
(Received via mailing list)
Hi,

@sess_user = User.find(:first, :conditions=>["id = ?",
session[:user_id])

Eric

Craig White wrote:
> or
> Rails@lists.rubyonrails.org
> http://lists.rubyonrails.org/mailman/listinfo/rails
>


--
Eric Goodwin
http://www.ericgoodwin.com
59de94a56fd2c198f33d9515d1c05961?d=identicon&s=25 Tom Mornini (Guest)
on 2006-02-16 01:40
(Received via mailing list)
On Feb 15, 2006, at 4:33 PM, Craig White wrote:

> or
> @sess_user = User.find([:first, "id = ?", session[:user_id])
>
> gives me an error...symbol as Array index

You don't need the [] around the arguments in find...and there's
a shortcut that you don't know about.

@sess_user = User.find(session[:user_id])

--
-- Tom Mornini
59de94a56fd2c198f33d9515d1c05961?d=identicon&s=25 Tom Mornini (Guest)
on 2006-02-16 01:43
(Received via mailing list)
On Feb 15, 2006, at 4:39 PM, Tom Mornini wrote:

>>
>> @sess_user = User.find([:first, "id = ?", session[:user_id])
>> or
>> @sess_user = User.find([:first, "id = ?", session[:user_id])
>>
>> gives me an error...symbol as Array index
>
> You don't need the [] around the arguments in find...and there's
> a shortcut that you don't know about.

Correction:

You have the [ in the wrong place. Needs to be after the comma.

Short form is better still...
C1e5a9e9344b6d31b9df7303e6dc378a?d=identicon&s=25 Craig White (Guest)
on 2006-02-16 01:52
(Received via mailing list)
Yeah...I got brackets out of whack...

  @sess_user = User.find(:first,
    :conditions => ["id = ?", session[:user_id] ])

or

  @sess_user = User.find(session[:user_id])

both still return the same error...

symbol as array index

which probably still isn't what I want to get out of it since I actually
want user.name from the 'find' but nevertheless...the error is still the
same.

Thanks

Craig
0e5e8a4176061ad36e3e31311e28e309?d=identicon&s=25 Estelle Winterflood (Guest)
on 2006-02-16 02:37
(Received via mailing list)
Craig,

Can you show us the exact contents of your session, or show us the
code for the user id being added to the session data?  It sounds like
there is a problem with session[:user_id]

Estelle.
C1e5a9e9344b6d31b9df7303e6dc378a?d=identicon&s=25 Craig White (Guest)
on 2006-02-16 03:47
(Received via mailing list)
On Wed, 2006-02-15 at 17:34 -0800, Estelle Winterflood wrote:
> Craig,
>
> Can you show us the exact contents of your session, or show us the
> code for the user id being added to the session data?  It sounds like
> there is a problem with session[:user_id]
>
----
I'm ashamed to tell you that I solved it entirely differently. When I
create the session, I get all the session variables I will need later
rather than resorting to having to get them out of the db later. This is
a simple process and it was my twisted logic that said...OK, I need this
value now and the only way I can get it is from the user_id in the
session.

Much simpler - thanks for the concern. I am now onto rights based roles
and I can see that Chad's 'Recipes' is gonna pay me dividends now.

Thanks

Craig
This topic is locked and can not be replied to.