0

Ryan Bates'/RailsCasts の Cookie ログインのチュートリアルに従い、作成中のアプリケーションの機能を思い出させてくれました。

[参照: http://railscasts.com/episodes/274-remember-me-reset-password?view=comments] 同じアプリケーションの OmniAuth 機能を紹介したいと思いました。

【参考: http: //railscasts.com/episodes/235-omniauth-part-1】devise は使っていません。Twitter アプリ ページを正しく読み込んでいますが、アプリケーションにリダイレクトするときにエラーが発生します。Twitter 内でコールバック URL が正しく設定されています。

OmniAuthundefined method "authentications" for nil:NilClassと authentications_controller でエラーが発生します。具体的には、私のコンソールには次のように表示されます。

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

認証コントローラーのコードは次のとおりです。

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

ユーザーには多くの認証があります。認証はユーザーに属します。

OmniAuth を Twitter で使用できるようにして、current_user コードを維持しながら受け取ったエラーを回避して、ユーザーが Twitter アカウントでログインできるようにしたいと考えています。

助けていただければ幸いです。

4

1 に答える 1

0

ユーザーがログインしていない場合を考慮していません。createアクションで行っていることは、サイト上のユーザーのアカウントを Twitter 上のユーザーのアカウントに関連付けることです。

current_userユーザーがログインしていることを確認せずに使用することはできません。それ以外の場合は が返されるnilため、undefined method authentications for nil:NilClass.

詳細については、次の Railscast をご覧ください: http://railscasts.com/episodes/236-omniauth-part-2

于 2012-03-30T00:44:34.877 に答える