簡體   English   中英

Ruby on Rails沒有這樣的列:authentication.provider:Omniauth

[英]Ruby on Rails no such column: authentication.provider: Omniauth

我在Omniauth上關注此教程: http: //railscasts.com/episodes/235-omniauth-part-1?view=asciicast

我不斷收到此錯誤:

沒有這樣的列:authentication.provider:

現在,我主要想知道的是為什么“提供者”未被接受。 它存在於類中...身份驗證數據庫存在...那么為什么說它不存在呢?

這是我的身份驗證控制器:

class AuthenticationsController < InheritedResources::Base
    def index
        @authentications = current_user.authentications if current_user
    end

    def create
        @user = User.where(authentication: auth).first_or_create(:provider => auth['provider'], :uid => auth['uid'])
        self.current_user = @user
        # auth = request.env["omniauth.auth"] current_user.authentications.create(:provider => auth['provider'], :uid => auth['uid'])
        flash[:notice] = "Authentication successful."
        redirect_to authentications_url
    end

    def auth
        request.env['omniauth.auth']
    end

    def destroy
        @authentication = current_user.authentications.find(params[:id])
        @authentication.destroy
        flash[:notice] = "Successfully destroyed authentication."
        redirect_to authentications_url
    end
end

我可以向您保證,我有一個稱為身份驗證的模型,並且該模型具有提供者和uid字段。 我也嘗試過where(authentications:auth)和where(auth:auth)都沒有運氣。

任何想法,將不勝感激。

更新

authentication.rb(模型)

class Authentication < ActiveRecord::Base
attr_accessible :create, :destroy, :index, :provider, :uid, :user_id
belongs_to :user
end

更新2

我基本上是在嘗試使本教程適應Rails 3.2。

該教程的原始行已在上方注釋掉。

更新3這是整個錯誤的第一行:

SQLite3 :: SQLException:沒有這樣的列:authentication.provider:選擇“ users”。*從“ users”位置“ authentication”。“ provider” ='facebook'AND“ authentication”。“ uid” ='2222222'AND“ authentication “。” info“ ='---!ruby / hash:OmniAuth :: AuthHash :: InfoHash

討厭成為負擔...但是時鍾真的在滴答作響,我的屁股在行,而我將變得非常瘋狂以試圖解決這個問題。 如果您能告訴我為什么不接受提供者的原因,那么我相信我會找出其余的原因。

你的創造動作沒有意義

User.where(authentication: auth)轉換為SELECT * FROM users WHERE authentication = a_hash

你應該做類似的事情

auth1 = Authentication.where(provider: auth['provider'], uid: auth['uid']).first
if !auth1.nil?
  current_user = auth.user
else
  user = User.new
  user.authentications.build(provider: auth['provider'], uid: auth['uid'])
  user.save!
  current_user = user
end

由於您只是在authentications表中添加一條記錄,因此我無法理解為什么要重新分配this.current_user 也是current_user是輔助方法還是成員,如果它是在其中聲明的成員?

您是否只想像這樣為當前用戶創建身份驗證?:

def create
    current_user.authentications.first_or_create(:provider => auth['provider'], :uid => auth['uid'])
    flash[:notice] = "Authentication successful."
    redirect_to authentications_url
end

這將按提供者和uid查找第一個身份驗證記錄,如果找不到,則創建該身份驗證記錄。

同樣由於該錯誤,我希望您已經找到了該問題的答案:

現在,我主要想知道的是為什么“提供者”未被接受。 它存在於類中...身份驗證數據庫存在...那么為什么說它不存在呢?

這是因為您是在User對象而不是Authentication上調用first_or_create()

我最近也遇到了這個問題。 起初我以為我忘記了在用戶表中添加一個提供者列,但事實並非如此。 這就是我最終解決它的方式:

def self.from_omniauth(auth)
  where(provider: auth["provider"], uid: auth["uid"]).first_or_create do |user|
    user.email = auth["info"]["email"]
    user.password = Devise.friendly_token[0, 20]
    user.logo = auth["info"]["image"]
    # if you use confirmable, since facebook validates emails
    # skip confirmation emails
    user.skip_confirmation!
  end
end

auth是一個類似於下面的哈希,因此我使用auth [“ provider”]代替了auth.provider:

omniauth.auth: {"provider"=>"facebook", "uid"=>"11111111111111", "info"=>{"email"=>"some@email.com", "image"=>"http://graph.facebook.com/v2.6/11111111111111/picture"}, "credentials"=>{"token"=>"sometoken", "expires_at"=>1506680013, "expires"=>true}, "extra"=>{"raw_info"=>{"email"=>"some@email.com", "id"=>"11111111111111"}}}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM