Lee Eather Lee Eather - 1 month ago 7
Ruby Question

Trying to define two different values for same variable in one method. rails

I am trying to define two different values to the one variable inside a sessions controller. I am getting "syntax error, unexpected keyword_elsif, expecting keyword_end" and "syntax error, unexpected end-of-input, expecting keyword_end". Obviously there is something wrong with elsif, or my grammar here. i am wondering what I am doing wrong or someone tell me a different way to do this.

def create
unless user = User.from_omniauth(env["omniauth.auth"])

user = User.find_by(email: params[:session][:email].downcase)
if user && user.authenticate(params[:session][:password])
log_in user
redirect_to user
# Log the user in and redirect to the user's show page.
else
# Create an error message.
flash.now[:danger] = 'Invalid email/password combination'
render 'new'
end

elsif
user = User.from_omniauth(env["omniauth.auth"])
log_in user
redirect_to user
end
end

RSB RSB
Answer

Try this,the error occurred because you were using elsif with unless which is not correct, you can only use else with unless.

def create
  user = User.from_omniauth(env["omniauth.auth"])      
  unless user.present?
    user = User.find_by(email: params[:session][:email].downcase)
    if user && user.authenticate(params[:session][:password])
      log_in user
      redirect_to user
      # Log the user in and redirect to the user's show page.
    else
      # Create an error message.
      flash.now[:danger] = 'Invalid email/password combination'
      render 'new'
    end    
  else        
    log_in user
    redirect_to user
  end
end

Hope that helps!