1

omn​​iauth をアプリケーションに統合しようとしていますが、devise を使用していません。私は Ryan Bates のスクリーン キャスト OmniAuth Part 2 #236 に従っていますが、彼は全員がデバイスを使用していると想定しています。authentication_controller.rb には、デバイス固有のコードがあります

   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'

誰かがこれの回避策を知っていますか...私はレールにかなり慣れていないので、一歩一歩が理想的です。

また、DEVISE を使用せずに OmniAuth を統合することをカバーする優れたチュートリアルを誰かが知っている場合も、感謝します。

4

1 に答える 1

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
于 2012-09-20T19:49:18.273 に答える