user id from the devise session

1,738 views
Skip to first unread message

Prizefighter

unread,
May 15, 2013, 3:12:37 PM5/15/13
to plataforma...@googlegroups.com
In my Rails app with Devise, I'm making some home-made analytics that works, in part, the following way. If a user visits another user's profile, the show action of the user's profile checks whether the visitor is a registered user and, if so, it saves the user_id of the visitor to the database. 

In my Rails 3 app, I could get the user_id of the visitor to the profile from the session like this

 session['warden.user.user.key'][1]

That would, for example, give me the number '16' for user with id 16. 

However, this didn't work when I remade my app with Rails 4. The first element of the array was no longer a number representing the user id. In my current Rails 4 app, i do

 session['warden.user.user.key'][0]

However, I'm concerned that the elements of the array don't always come out in the same order. Therefore, this code  session['warden.user.user.key'][0] might not work sometimes. 

When I look at the session, however, I also notice that there is in several places a hash structure with a user_id like this

"user_id"=>16

 but I can't figure out how to access it


Can anyone recommend what the most reliable way to get the user_id from the session is?

Walter Lee Davis

unread,
May 15, 2013, 3:18:36 PM5/15/13
to plataforma...@googlegroups.com
Could you try current_user.id ? That would seem to be the canonical way to do this.

Walter

Prizefighter

unread,
May 15, 2013, 3:26:21 PM5/15/13
to plataforma...@googlegroups.com
oops, yes, thanks, god I'm stupid sometimes
Reply all
Reply to author
Forward
0 new messages