![](/img/trans.png)
[英]Omniauth-facebook undefined method `slice' for nil:NilClass. NoMethodError in SessionsController#create
[英]login using omniauth-facebook “undefined method `slice' for nil:NilClass” error
我在使用omniauth-facebook時遇到問題。 我試圖使用Facebook登錄我的Rails應用程序,但它給了我一個錯誤,並且env ['omniauth.auth']拋出nil值,這是錯誤。
NoMethodError at /auth/facebook/callback
undefined method `slice' for nil:NilClass
這是我的模特
#fields
field :provider
field :uid
field :name
field :oauth_token
field :oauth_expires_at , type: DateTime
#functions
def self.from_omniauth(auth)
where(auth.slice(:provider, :uid)).find_or_initialize_by.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
這是我的控制器
def create
user = Usersfb.from_omniauth(ENV["omniauth.auth"])
session[:user_id] = user.id
redirect_to root_url
end
def destroy
session[:user_id] = nil
redirect_to root_url
end
我的routes.rb
root 'main#index'
get 'session/destroy' , to: 'session#destroy'
get 'auth/:provider/callback' => 'session#create'
request.env不適用於我。 我向omniauth初始化程序添加了callback_path,並且現在可以正常工作。 看起來是這樣的:
omniauth.rb
Rails.application.config.middleware.use OmniAuth::Builder do
provider :facebook, APP_ID, APP_SECRET, callback_path: '/auth/facebook/callback'
end
您在控制器操作中有錯誤:
def create
#user = Usersfb.from_omniauth(ENV["omniauth.auth"])
user = Usersfb.from_omniauth(request.env["omniauth.auth"])
session[:user_id] = user.id
redirect_to root_url
end
ENV
是環境變量而不是request.env
,此ENV["omniauth.auth"]
返回nil
而where(auth.slice(:provider, :uid))
嘗試切片nil
會undefined method 'slice' for nil:NilClass
引發undefined method 'slice' for nil:NilClass
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.