Hello, I got a session problem that I can''t solve. session[:test] = "test session" works fine and can be printed from the view. session[:test] = User.find(1) returns true but the session is empty in the view! @data={:test=>#}, ... Session config: config.action_controller.session = { :session_key => ''_www_session'', :secret => ''key removed..'' } config.action_controller.session_store = :active_record_store I found a thread on the RoR wiki that I should put model :user in application.rb but that gives me an error. (depricated?) Best regards. Asbjørn Morell. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
You''re better off storing the id in the session, and reloading from that. I don''t think that session supports saving entire objects without serializing / deserializing them. I''ve actually never tried to do this, because it''s a bad practic e (IMHO). Julian. Learn Ruby on Rails! Check out the FREE VIDS (for a limited time) VIDEO #3 out NOW! http://sensei.zenunit.com/ On 08/04/2008, at 7:56 PM, atmorell wrote:> > Hello, > > I got a session problem that I can''t solve. > > session[:test] = "test session" works fine and can be printed from the > view. > > session[:test] = User.find(1) returns true but the session is empty in > the view! > @data={:test=>#}, ... > > Session config: > config.action_controller.session = { > :session_key => ''_www_session'', > :secret => ''key removed..'' > } > > config.action_controller.session_store = :active_record_store > > I found a thread on the RoR wiki that I should put model :user in > application.rb but that gives me an error. (depricated?) > > Best regards. > Asbjørn Morell. > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Hey Julian, Could you show me how I only store the id? That''s actually what I am trying to do :) This only fech the id, but does not work :/ @user_id = find(:first, :select => ["id"], :conditions => ["login = ? and password = ?", login, Digest::SHA1.hexdigest(password)]) Checked the log and the query is correct and only return ex. 1 Best regards. Asbjørn Morell. On Apr 8, 12:59 pm, Julian Leviston <jul...-AfxEtdRqmE/tt0EhB6fy4g@public.gmane.org> wrote:> You''re better off storing the id in the session, and reloading from > that. I don''t think that session supports saving entire objects > without serializing / deserializing them. I''ve actually never tried to > do this, because it''s a bad practic e (IMHO). > > Julian. > > Learn Ruby on Rails! Check out the FREE VIDS (for a limited time) > VIDEO #3 out NOW!http://sensei.zenunit.com/ > > On 08/04/2008, at 7:56 PM, atmorell wrote: > > > > > > > Hello, > > > I got a session problem that I can''t solve. > > > session[:test] = "test session" works fine and can be printed from the > > view. > > > session[:test] = User.find(1) returns true but the session is empty in > > the view! > > @data={:test=>#}, ... > > > Session config: > > config.action_controller.session = { > > :session_key => ''_www_session'', > > :secret => ''key removed..'' > > } > > > config.action_controller.session_store = :active_record_store > > > I found a thread on the RoR wiki that I should put model :user in > > application.rb but that gives me an error. (depricated?) > > > Best regards. > > Asbjørn Morell.- Hide quoted text - > > - Show quoted text ---~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On 8 Apr 2008, at 10:56, atmorell wrote:> > Hello, > > I got a session problem that I can''t solve. > > session[:test] = "test session" works fine and can be printed from the > view. > > session[:test] = User.find(1) returns true but the session is empty in > the view! > @data={:test=>#}, ...The default to_s for an active record object looks like #<User: .... > which most browsers render as just #. If in your view you put <%= h session[:test] %> you''d probably see what you were expecting As Julian said putting whole model objects in the session isn''t a great idea. If @user was your user you''d just stick @user.id in the session. Fred> Session config: > config.action_controller.session = { > :session_key => ''_www_session'', > :secret => ''key removed..'' > } > > config.action_controller.session_store = :active_record_store > > I found a thread on the RoR wiki that I should put model :user in > application.rb but that gives me an error. (depricated?) > > Best regards. > Asbjørn Morell. > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Okay. Thanks. I got that working now :) How do I access an object from the view loaded by an application.rb before_filter? class ApplicationController < ActionController::Base before_filter :user def user if session[:user] @user = User.find(session[:user]) end end end @user is working fine from my controllers but not in my views :/ Best regards. Asbjørn Morell. On Apr 8, 2:18 pm, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On 8 Apr 2008, at 10:56, atmorell wrote: > > > > > Hello, > > > I got a session problem that I can''t solve. > > > session[:test] = "test session" works fine and can be printed from the > > view. > > > session[:test] = User.find(1) returns true but the session is empty in > > the view! > > @data={:test=>#}, ... > > The default to_s for an active record object looks like #<User: .... > > which most browsers render as just #. If in your view you put <%= h > session[:test] %> you''d probably see what you were expecting > As Julian said putting whole model objects in the session isn''t a > great idea. If @user was your user you''d just stick @user.id in the > session. > > Fred > > > > > Session config: > > config.action_controller.session = { > > :session_key => ''_www_session'', > > :secret => ''key removed..'' > > } > > > config.action_controller.session_store = :active_record_store > > > I found a thread on the RoR wiki that I should put model :user in > > application.rb but that gives me an error. (depricated?) > > > Best regards. > > Asbjørn Morell.- Hide quoted text - > > - Show quoted text ---~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Yeah, that won''t work. You have no model on your find command. That makes no sense to rails (active record, here). Say I have a users table, and a User model. @user_id = User.find(:first).id or user = User.find(:first) @user_id = user.id Either of those will find you the id. ActiveRecord::Base.find will find a row, and create an object out of it, so you need to ask the object what it''s ID is by sending it the id message, as above. Julian. Learn Ruby on Rails! Check out the FREE VIDS (for a limited time) VIDEO #3 out NOW! http://sensei.zenunit.com/ On 08/04/2008, at 10:11 PM, atmorell wrote:> > Hey Julian, > > Could you show me how I only store the id? That''s actually what I am > trying to do :) > > This only fech the id, but does not work :/ > @user_id = find(:first, :select => ["id"], :conditions => ["login = ? > and password = ?", login, Digest::SHA1.hexdigest(password)]) > > Checked the log and the query is correct and only return ex. 1 > > Best regards. > Asbjørn Morell. > > > On Apr 8, 12:59 pm, Julian Leviston <jul...-AfxEtdRqmE/tt0EhB6fy4g@public.gmane.org> wrote: >> You''re better off storing the id in the session, and reloading from >> that. I don''t think that session supports saving entire objects >> without serializing / deserializing them. I''ve actually never tried >> to >> do this, because it''s a bad practic e (IMHO). >> >> Julian. >> >> Learn Ruby on Rails! Check out the FREE VIDS (for a limited time) >> VIDEO #3 out NOW!http://sensei.zenunit.com/ >> >> On 08/04/2008, at 7:56 PM, atmorell wrote: >> >> >> >> >> >>> Hello, >> >>> I got a session problem that I can''t solve. >> >>> session[:test] = "test session" works fine and can be printed from >>> the >>> view. >> >>> session[:test] = User.find(1) returns true but the session is >>> empty in >>> the view! >>> @data={:test=>#}, ... >> >>> Session config: >>> config.action_controller.session = { >>> :session_key => ''_www_session'', >>> :secret => ''key removed..'' >>> } >> >>> config.action_controller.session_store = :active_record_store >> >>> I found a thread on the RoR wiki that I should put model :user in >>> application.rb but that gives me an error. (depricated?) >> >>> Best regards. >>> Asbjørn Morell.- Hide quoted text - >> >> - Show quoted text - > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---