2012-03-29 1 views
0

쿠키 로그인을 위해 Ryan Bates의/RailsCast 자습서를 읽고 필자가 작성중인 응용 프로그램의 기능을 생각 나게합니다.Remind Me (episode 274)와 OmniAuth RailsCast (에피소드 275) 문제

[참조 : http://railscasts.com/episodes/274-remember-me-reset-password?view=comments] 동일한 응용 프로그램에 대해 OmniAuth 기능을 소개하고자했습니다.

[참조 : http://railscasts.com/episodes/235-omniauth-part-1] 나는 장치를 사용하지 않는다. Twitter 응용 프로그램 페이지를 제대로로드하고 있습니다. 응용 프로그램으로 리디렉션 할 때 오류가 발생합니다. Twitter 내에서 콜백 URL을 올바르게 설정했습니다.

OmniAuth 및 my authentications_controller에서 undefined method "authentications" for nil:NilClass 오류가 발생합니다. 여기

NoMethodError (undefined method authentications for nil:NilClass):app/controllers/authentications_controller.rb:9:in create'.

을의 Authentications 컨트롤러 코드 : 특히 내 콘솔 읽기

여기
class AuthenticationsController < ApplicationController 

    def index 
    authentications = Authentication.all 
    end 

    def create 
    auth = request.env["omniauth.auth"] 
    current_user.authentications.create(:provider => auth['provider'], :uid => auth['uid']) 
    flash[:notice] = "Authentication was successful." 
    redirect_to authentications_url 
    end 

내 응용 프로그램 컨트롤러에서 CURRENT_USER 도우미 방법이다.

private 
    def current_user 
    @current_user ||= User.find_by_auth_token!(cookies[:auth_token]) if cookies[:auth_token] 
    end 
    helper_method :current_user 

사용자는 많은 인증을 받았습니다. 인증은 사용자에게 속합니다.

나는 사용자가

도움말이 감사 내 CURRENT_USER 코드를 유지하면서 내가 수신하고 오류를 피함으로써 자신의 트위터 계정으로 로그인 할 수 있도록 트위터와 함께 작동하도록 OmniAuth 수 있도록하고 싶습니다.

답변

0

사용자가 로그인하지 않은 경우를 고려하지 않고 있습니다. create 작업에서 수행중인 작업은 사이트의 사용자 계정을 Twitter의 계정에 연결하는 것입니다.

사용자가 로그인하지 않고 current_user을 사용할 수 없습니다. 그렇지 않으면 nil이 반환되므로 undefined method authentications for nil:NilClass이 표시됩니다.

자세한 내용은 다음 레일 스 캐스트를 참조하십시오. http://railscasts.com/episodes/236-omniauth-part-2