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 アカウントでログインできるようにしたいと考えています。
助けていただければ幸いです。