Before_filter: nil vs. true vs. false

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 R. 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

On Jun 30, 2006, at 3:32 PM, Chris H. wrote:

In Ruby, only false and nil are false, anything else is true

Great. 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 R. 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.

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/

Thanks Philip!

On Jun 30, 2006, at 4:27 PM, Philip R. wrote:

Only returning false will halt processing. See:

http://api.rubyonrails.com/classes/ActionController/Filters/
ClassMethods.html


http://wagerlabs.com/

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

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 H. 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/

Joel R. 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 R.
http://tzinfo.rubyforge.org/ – DST-aware timezone library for Ruby

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs