Hi all, I have been using rails for about a week and so far it has been good fun. I have got stuck on something that should be very simple - testing if a user is logged-in in the view: <% if @session[''user''] == nil? %> <li id="menu_login"><a href="/user/login" title="Login">Login</a></li> <% else %> <li id="menu_logout"><a href="/user/logout" title="Logout">Logout</a></li> <% end %> always gives a logout link! The user is definitely not in the session as <%= debug @session %> gives the following: --- !ruby/object:CGI::Session data: &id001 flash: !ruby/hash:ActionController::Flash::FlashHash {} dbman: &id002 !ruby/object:CGI::Session::PStore hash: *id001 p: !ruby/object:PStore abort: false filename: /tmp/ruby_sess.08c6dca283b7e3bb rdonly: false table: transaction: false dbprot: - *id002 new_session: false session_id: 5ad5df8c51f03e3a98544e5f4c48e2b7 I feel that I must be missing something very simple but can''t see how to fix it. I would be grateful for any help. Thanks, David
On Wednesday, April 6, 2005, 12:03:16 AM, David wrote:> Hi all,> I have been using rails for about a week and so far it has been good > fun. I have got stuck on something that should be very simple - testing > if a user is logged-in in the view:> <% if @session[''user''] == nil? %>Subtle. Try <% if @session[''user''] == nil %> You''re not supposed to insert a question mark at the end of an if statement. Most times you would get an error, but it so happens that "nil?" is meaningful. I reckon that''s your problem. BTW, you may want to throw this in the ApplicationHelper class: def logged_in? @session[''user''] != nil end Then you can use <% if logged_in? %> in the view. Cheers, Gavin
On Wed, 2005-04-06 at 00:11 +1000, Gavin Sinclair wrote:> > I have got stuck on something that should be very simple - testing > > if a user is logged-in in the view: > > <% if @session[''user''] == nil? %> > > Subtle. Try > > <% if @session[''user''] == nil %>Thanks for your help - I think it''s time to actually learn ruby :) David
On Apr 5, 2005 9:27 AM, David Blundell <david.blundell-7ie3GnOy+6yuWcBOZErdyg@public.gmane.org> wrote:> On Wed, 2005-04-06 at 00:11 +1000, Gavin Sinclair wrote: > > > I have got stuck on something that should be very simple - testing > > > if a user is logged-in in the view: > > > <% if @session[''user''] == nil? %> > > > > Subtle. Try > > > > <% if @session[''user''] == nil %> > > Thanks for your help - I think it''s time to actually learn ruby :)Just wanted to add that you can also do <% if @session[''user''].nil? %> Coming from C#, it''s weird to see that even nil is an object in Ruby... -- rick http://techno-weenie.net
On Wednesday, April 6, 2005, 1:16:55 AM, Wolfgang wrote:> Hi!> On Tue, 05 Apr 2005, Rick Olson wrote the following: >> <% if @session[''user''].nil? %> >> >> Coming from C#, it''s weird to see that even nil is an object in Ruby...> "nil?" is a method of class NilClass > http://www.rubycentral.com/book/ref_c_nilclass.htmlAnd of class Object. If it were written in Ruby: class Object def nil?; false; end end class NilClass def nil?; true; end end Gavin
Hi! On Tue, 05 Apr 2005, Rick Olson wrote the following:> <% if @session[''user''].nil? %> > > Coming from C#, it''s weird to see that even nil is an object in Ruby..."nil?" is a method of class NilClass http://www.rubycentral.com/book/ref_c_nilclass.html Wolfgang
On Apr 5, 2005 8:03 AM, David Blundell <david.blundell-7ie3GnOy+6yuWcBOZErdyg@public.gmane.org> wrote:> Hi all, > > I have been using rails for about a week and so far it has been good > fun. I have got stuck on something that should be very simple - testing > if a user is logged-in in the view: > > <% if @session[''user''] == nil? %> > <li id="menu_login"><a href="/user/login" title="Login">Login</a></li> > <% else %> > <li id="menu_logout"><a href="/user/logout" title="Logout">Logout</a></li> > <% end %> > > always gives a logout link! > > The user is definitely not in the session as <%= debug @session %> gives > the following: > > --- !ruby/object:CGI::Session > data: &id001 > flash: !ruby/hash:ActionController::Flash::FlashHash {} > dbman: &id002 !ruby/object:CGI::Session::PStore > hash: *id001 > p: !ruby/object:PStore > abort: false > filename: /tmp/ruby_sess.08c6dca283b7e3bb > rdonly: false > table: > transaction: false > dbprot: > - *id002 > new_session: false > session_id: 5ad5df8c51f03e3a98544e5f4c48e2b7 > > I feel that I must be missing something very simple but can''t see how to > fix it. I would be grateful for any help. > > Thanks, > > David > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >One small suggestion (as the problem has already been solved): Moving this logic to a helper method, probably in ApplicationHelper so that it''s available to all views, would be adviseable. In other words, you''d still have an if statement in your view, but you''d be testing the result of a helper function instead of the very specific situation where you are assuming that the application will ALWAYS place the current user as ''user'' in the session. If you change how this is stored in the future, you will only have to change it in one place instead of in many views. Dave -- Dave Goodlad dgoodlad-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org or dave-eHfbeeWWzZOw5LPnMra/2Q@public.gmane.org http://david.goodlad.ca/