开发者

Finding User ID from Sessions

My question is simi开发者_如何学Clar to this question. Though I have not been able to figure it out yet. I want to be able to find the user id from the session. I understand that this can only be done within the controller, though I need this for a model method called Activity.log...

In my controller, I have the following:

@current_user = User.find(session[:user_id])
user_id = @current_user.id
Activity.log(user_id)

I hope that I am at least on the right track, and from the other SO question I have linked to, I have assumed that sessions is still the best way to achieve this, though I am open to suggestions. Thanks!


As indicated by @Bohdan, you may want to shorten your code a bit like this:

Activity.log( session[:user_id] )

In your case, you don't have to rely on ActiveRecord mechanisms.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜