Friday, 15 April 2011

ruby on rails - undefined method `id' for false:FalseClass -



ruby on rails - undefined method `id' for false:FalseClass -

the error (undefined method 'id' false:falseclass) referencing line:

if current_user && current_user.admin? || current_user.id == id.to_i

why producing error? shouldn't not reach current_user.id if first && status fails?

the expression

current_user && current_user.admin? || current_user.id == id.to_i

is parsed following

(current_user && current_user.admin?) || (current_user.id == id.to_i)

so, when current_user false, evaluation goes like

(false && current_user.admin?) || ? (false) || ? (false.id == id.to_i) # \\ kaboom! //

it should expressed following, parenthesis, in methods invoked against current_user if it's not false/nil.

current_user && (current_user.admin? || current_user.id == id.to_i)

alternatively, consider using other flow command structures if and/or discrete methods encapsulating logic.

it odd current_user can false: nil more fitting when there "no current user"..

ruby-on-rails ruby

No comments:

Post a Comment