2016-11-06 20 views
-1

我想在會話控制器中爲一個變量定義兩個不同的值。我得到了「語法錯誤,意外的關鍵字關鍵字,期待keyword_end」和「語法錯誤,意想不到的結束輸入,期待keyword_end」。很明顯,elsif或者我的語法在這裏出了問題。我想知道我做錯了什麼,或者有人告訴我以不同的方式來做到這一點。嘗試在一種方法中爲相同變量定義兩個不同的值。 rails

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 
+0

嘗試過這一點,結果仍然相同。 –

回答

3

試試這個,因爲你使用elsifunless這是不正確的錯誤發生,你只能使用else與不在此限。

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 

希望有所幫助!

+0

熱的織物。你的權利。謝謝您的幫助。我必須明白這是如何工作的。 –

+0

發生錯誤的原因是因爲您使用'elsif'除非'這是不正確的,您只能使用'else'除非,編輯我的答案和解釋。 – RSB

+0

啊好的。我不明白語法爲什麼它不是user.present?是問用戶是否在場我以爲已經是? –

相關問題