0

我下面這個教程Omniauth:http://railscasts.com/episodes/235-omniauth-part-1?view=asciicastRuby on Rails的沒有這樣的列:authentication.provider:Omniauth

我不斷收到此錯誤:

no such column: 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場。我也嘗試過每個地方(認證:auth)和哪裏(auth:auth) ,沒有運氣。

任何想法,將不勝感激。

UPDATE

authentication.rb(模型)

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

更新2

基本上,我試圖適應本教程的軌道3.2。

本教程中的原始行被註釋掉。

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

SQLite3::SQLException: no such column: authentication.provider: SELECT "users".* FROM "users" WHERE "authentication"."provider" = 'facebook' AND "authentication"."uid" = '2222222' AND "authentication"."info" = '--- !ruby/hash:OmniAuth::AuthHash::InfoHash

不想成爲負擔......但是,時鐘的滴答作響真的,我的屁股就行了,我即將徹底瘋狂試圖找出這一點。如果你能告訴我爲什麼供應商沒有被接受,我相信我能弄清楚其餘的。

+0

貴'Authentication'模型具有'provider'屬性?請向我們展示您的'Authentication'模型的註釋。 – vee

+0

如果您向我們保證模型'Authentication'具有'provider'屬性,請發佈您的錯誤回溯。 – vee

+0

更新了我的身份驗證模型。 –

回答

0

您創建的行動並沒有感覺到

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 
+0

有了這個,我得到了「未定義的方法'[]'爲零:NilClass」 –

+0

Umm .. 。 你好? :)任何想法爲什麼它說未定義的方法「無」?每當我嘗試使用「if」語句出於某種奇怪的原因時,我都會得到... –

+0

auth是否存在?我已經改變了使用不同的var名稱的答案 – Aguardientico

0

因爲你只是在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找到的第一個驗證記錄,如果未找到,然後創建認證記錄。

此外,通過該錯誤,我希望你已經想通了這個問題的答案:

Now the main thing I want to know is why "provider" isn't being accepted. It exists in the class... the authentications database exists... so why is it saying it isn't there?

這是因爲你在User對象,而不是Authentication調用first_or_create()

0

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

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.provider,我用AUTH [「提供者」]等:

omniauth.auth: {"provider"=>"facebook", "uid"=>"11111111111111", "info"=>{"email"=>"[email protected]", "image"=>"http://graph.facebook.com/v2.6/11111111111111/picture"}, "credentials"=>{"token"=>"sometoken", "expires_at"=>1506680013, "expires"=>true}, "extra"=>{"raw_info"=>{"email"=>"[email protected]", "id"=>"11111111111111"}}} 
相關問題