2012-09-20 54 views
1

嘿即時嘗試將omniauth集成到我的應用程序,但我不使用設計。我跟隨瑞恩貝茨的屏幕投了OmniAuth第2部分#236,他假設每個人都在使用設計。在authentication_controller.rb還有就是制訂具體omniauth without devise rails cast 236

def create 
omniauth = request.env["omniauth.auth"] 
authentication = Authentication.find_by_provider_and_uid(omniauth['provider'], omniauth['uid']) 
if authentication 
    flash[:notice] = "Signed in successfully." 
    sign_in_and_redirect(:user, authentication.user) 
elsif current_user 
    current_user.authentications.create!(:provider => omniauth['provider'], :uid => omniauth['uid']) 
    flash[:notice] = "Authentication successful." 
    redirect_to authentications_url 
else 
    user = User.new 
    user.apply_omniauth(omniauth) 
if user.save 
    flash[:notice] = "Signed in successfully." 
    sign_in_and_redirect(:user, user) 
    else 
    session[:omniauth] = omniauth.except('extra') 
    redirect_to new_user_registration_url 
    end 
end 

結束的代碼

其sign_in_and_redirect

當我刷新我的網頁,我收到了

undefined method `sign_in_and_redirect' 

沒有人知道一個解決這個問題的方法...即時通訊非常新的軌道,所以一步一步將是理想的。

謝謝你們,如果有人知道一個很好的教程,涵蓋集成OmniAuth沒有DEVISE會很好。

回答

2

我建議在看這些軌道/ ASCII投:

http://railscasts.com/episodes/241-simple-omniauth

http://asciicasts.com/episodes/304-omniauth-identity

sign_in_and_redirect是要設置在會話中的當前用戶,以及其他任何你想要做的跡象,然後重定向到首頁,或成功登錄後設置爲頁面的任何內容。

相反,做自己這裏,像這樣也許:

def create 
    authentication = Authentication.find_by_provider_and_uid(omniauth['provider'], omniauth['uid']) 
    if authentication 
     flash[:notice] = "Signed in successfully." 
     session[:user_id] = authentication.user.id 
     redirect_to root_url, notice: "Signed in!" 
    end 
    end 

然後,在應用程序控制器,是這樣的:

def current_user 
    User.find(session[:user_id]) if logged_in? 
end 

def logged_in? 
    !!session[:user_id] 
end 
+0

嘿感謝您的建議,但不登入的用戶它只是重定向到主頁 – user1502223

+0

設置會話[:user_id]是什麼設置用戶登錄。 –

+0

感謝您的鏈接,但我會穿過他們嘗試不同的方法。 – user1502223

相關問題