Hi, everyone.
I used a session in my model. but when I test this model. There said
Error:
NameError: undefined local variable or method `session’ for
UserSetting:Class
In UserSetting model, I use session like this:
…
language= session[:lang]
…
why? who know that, please tell me. Thanks!
Been awhile, but session is local to the controller not the model.
If you want the model to use the session, you’ll have to pass the
session to the model.
OnRails wrote:
Hi, everyone.
I used a session in my model. but when I test this model. There said
Error:
NameError: undefined local variable or method `session’ for
UserSetting:Class
In UserSetting model, I use session like this:
…
language= session[:lang]
…
why? who know that, please tell me. Thanks!
Thanks David H.!
but I don’t know how to pass the session to the model, could you tell
me how to do that?
Thanks!
Something like:
class MyModel < AR::Base
def do_something(session)
…
end
end
and in a controller:
m = MyModel.new
m.do_something(session)
Keep in mind, though, that models aren’t really supposed to have a
sense of a “session”. They just sort of sit there and do their thing.
The controller should handle (i.e., control) the management of actual
connections and sessions.
David
–
Upcoming training by David A. Black/Ruby Power and Light, LLC:
* Intro to Rails, London, UK, December 3-6 (by Skills Matter)
See http://www.rubypal.com for details and 2008 announcements!
On Fri, Nov 30, 2007 at 12:16:43PM -0800, OnRails wrote:
Thanks David H.!
but I don’t know how to pass the session to the model, could you tell
me how to do that?
This is a fundamental design mistake. Your model layer should be
entirely
independent of the layers above it. Think about what you need from the
session, why you think you need it, and how you could handle it without
muddying the highly desirable separation of layers.