ruby on rails 4 - Setting a variable equal to a function and if statements -


i new ruby language , ruby on rails, i'm not entirely sure how word question (and use incorrect terminology). following code not work

@user = user.find_by email: params[:session][:email]  if @user.authenticate(params[:session][:password])       # signin @user  else      # render failed signin  end  

i error method authenticate not defined nil:nilclass, following code works fine:

@user = user.find_by email: params[:session][:email]  if @user && @user.authenticate(params[:session][:password])      # signin @user  else      # render failed signin  end  

i don't understand why first code block doesn't work. when define @user user.find_by method not run , set value of @user? or variables value set when called in program?

edit: unnecessary parentheses removed.

here's think wrong first block:

in first block, search user , if not found return nil. then, trying authenticate nil , that's why giving not defined nil:class.

on second block fixing problem adding @user in if block. testing see whether have retrieved user first, incase user not found block not executed.


Comments

Popular posts from this blog

basic authentication with http post params android -

vb.net - Virtual Keyboard commands -

How to get multiresult with multicondition in Sql Server -