Satchel
Satchel

Reputation: 16724

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['extra']['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?

Upvotes: 0

Views: 570

Answers (2)

dombesz
dombesz

Reputation: 7899

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.

Upvotes: 0

Spyros
Spyros

Reputation: 48616

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

Upvotes: 1

Related Questions