Folks, My understanding of how filters work is that I should return true if everything is ok and false if not. How does the filter below work then (from the Rails Recipies book)? It would return nil if there''s a user in the session. Does nil count as true? before_filter :check_authentication def check_authentication unless session[:user] session[:intended_action] = action_name session[:intended_controller] = controller_name redirect_to :action => "signin" end end Thanks, Joel -- http://wagerlabs.com/
Joel Reymont wrote:> It would return nil if there''s a user in the session. Does nil count > as true?In Ruby, only false and nil are false, anything else is true Cheers -- Posted via http://www.ruby-forum.com/.
On Jun 30, 2006, at 3:32 PM, Chris Hulan wrote:> In Ruby, only false and nil are false, anything else is trueGreat. So that means that the following will return false when there''s a user in the session and filter processing will stop (action will be aborted?). Isn''t this filter missing a "return true" as its last statement? def check_authentication unless session[:user] session[:intended_action] = action_name session[:intended_controller] = controller_name redirect_to :action => "signin" end end -- http://wagerlabs.com/
Joel Reymont wrote:> Great. So that means that the following will return false when > there''s a user in the session"unless" evaluates an expression to true or false... so in the above if a user is logged in session[:user] will return true because it''s not nil -- it exists and is therefore not false. There''s no need for a "return true" because the code block won''t be executed if session[:user] exists. -- Posted via http://www.ruby-forum.com/.
On Jun 30, 2006, at 3:57 PM, Guest wrote:> "unless" evaluates an expression to true or false... so in the > above if > a user is logged in session[:user] will return true because it''s > not nil > -- it exists and is therefore not false. There''s no need for a "return > true" because the code block won''t be executed if session[:user] > exists.I ran "true unless true" at the irb prompt and got nil so I assumed that the result of the filter would be nil so long as the unless block does not execute. Are you saying that the return value will be different if the unless block is not triggered? What is the resulting value of the trigger then? Thanks, Joel -- http://wagerlabs.com/
Joel Reymont wrote:> Folks, > > My understanding of how filters work is that I should return true if > everything is ok and false if not. How does the filter below work then > (from the Rails Recipies book)? > > It would return nil if there''s a user in the session. Does nil count as > true?Only returning false will halt processing. See: http://api.rubyonrails.com/classes/ActionController/Filters/ClassMethods.html -- Philip Ross http://tzinfo.rubyforge.org/ -- DST-aware timezone library for Ruby
Thanks Philip! On Jun 30, 2006, at 4:27 PM, Philip Ross wrote:> Only returning false will halt processing. See: > > http://api.rubyonrails.com/classes/ActionController/Filters/ > ClassMethods.html-- http://wagerlabs.com/
Chris Hulan
2006-Jun-30 17:15 UTC
[Rails] Re: Re: Re: before_filter: nil vs. true vs. false
> Are you saying that the return value will be different if the unless > block is not triggered? What is the resulting value of the trigger then?If the unless block is executed the return value will be the value of the last statement. So whatever is returned from ''redirect_to :action => "signin"'' will be the returned. Cheers -- Posted via http://www.ruby-forum.com/.
Joel Reymont
2006-Jun-30 17:32 UTC
[Rails] Re: Re: Re: before_filter: nil vs. true vs. false
I meant the resulting value of the "filter" rather than "trigger". It''s clearly nil if unless is not executed but the filter system only takes action on false so it does not matter. On Jun 30, 2006, at 6:15 PM, Chris Hulan wrote:> If the unless block is executed the return value will be the value of > the last statement. So whatever is returned from ''redirect_to :action > => "signin"'' will be the returned.-- http://wagerlabs.com/