开发者

Can I store a session variable from a method inside a model in Rails 3?

I tried to do the following:

 20       session[:atoken] = linked_in_data['extra']['access_token'].token
 21       session[:asecret] = linked_in_data['extr开发者_如何学Ca']['access_token'].secre

t

This is within a method inside of a User model.

But I get an error saying undefined method for session...why? Can session variables only be set in a controller?


It's a bad practice, but if you must do it :

http://m.onkey.org/how-to-access-session-cookies-params-request-in-model

But, finding a workaround is always better. Take a look at that as well :

http://media.railscasts.com/videos/119_session_based_model.mov


The session can't be accessed in the models. It would break somehow the MVC structure of the app. If you want to change the session values during save, update etc..., you can try to use sweeper.See the api. You can access the model's attributes, and the session as well, and you can observe changes of the object.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜