2

このチュートリアルに正確に従いましたが、次のエラーが発生しています。

NoMethodError in SessionsController#create
undefined method `slice' for nil:NilClass
Rails.root: /Users/raybesiga/Documents/Sites/foodie
Application Trace | Framework Trace | Full Trace
app/models/user.rb:29:in from_omniauth'
app/controllers/sessions_controller.rb:3:increate'

ただし、私のuser.rbファイルは次のとおりです。

class User < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :token_authenticatable, :confirmable,
  # :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable

  # Setup accessible (or protected) attributes for your model
  attr_accessible :name, :email, :password, :password_confirmation, :remember_me,     :provider, :uid
  validates_presence_of :name
  validates_uniqueness_of :name, :email, :case_sensitive => false
  # attr_accessible :title, :body

  def self.from_omniauth(auth)
    where(auth.slice(:provider, :uid)).first_or_initialize.tap do |user|
      user.provider = auth.provider
      user.uid = auth.uid
      user.name = auth.info.name
      user.oauth_token = auth.credentials.token
      user.oauth_expires_at = Time.at(auth.credentials.expires_at)
      user.save!
    end
  end
end

私のsessions_controller.rbファイルは次のとおりです。

class SessionsController < ApplicationController
        def create
                user = User.from_omniauth(env["ominauth.auth"])
                # session[:user_id] = user.user_id
                session[:user_id] = user.id
                redirect_to root_url
        end

        def destroy
                session[:user_id] = nil
                redirect_to root_url
        end
end

このエラーが発生する理由は何ですか?

4

1 に答える 1

1

セッションコントローラーのスペルがenv['omniauth.auth']間違っています。あなたが置くenv['ominauth.auth']

于 2012-08-28T15:13:25.157 に答える