2012-02-21 58 views
2

我已經設置了Omniauth Facebook身份驗證根據本教程:http://net.tutsplus.com/tutorials/ruby/how-to-use-omniauth-to-authenticate-your-users/ 現在我試圖結合它與omniauth身份使用相同的用戶模型,而不是單獨的身份模型,如本教程:http://railscasts.com/episodes/304-omniauth-identity?view=asciicast,但我無法讓它正常工作。Omniauth Facebook身份驗證+身份使用相同的模型,而不是兩個

這是我的初始化/ omniauth.rb文件:

Rails.application.config.middleware.use OmniAuth::Builder do 
    provider :facebook, 'xxxxx', 'xxxxx' 
    provider :identity, :fields => [:email], :model => User 
end 

我添加了由omniauth身份需要我的用戶模型/桌,改變了用戶模型代碼「password_digest」列

class User < ActiveRecord::Base 
    has_many :authorizations 
    #validates :name, :email, :presence => true 

    def add_provider(auth_hash) 
    # check if the provider already exists, so we don't add it twice 
    unless authorizations.find_by_provider_and_uid(auth_hash["provider"], auth_hash["uid"]) 
     Authorization.create :user => self, :provider => auth_hash["provider"], :uid => auth_hash["uid"], :token => auth_hash["token"] 
    end 
    end 
end 

class User < OmniAuth::Identity::Models::ActiveRecord 
    ... 
end 

但是當我這樣做時,創建用戶和授權模型的授權模型中的代碼無法正常工作 當用戶模型從ActiveRecord :: Base擴展時,記錄創建得很好,但是當我擴展用戶模型從OmniAuth :: Identity :: Models :: ActiveRecord創建新的授權時,用戶模型不會存儲在數據庫中。

這是授權型號代碼:

class Authorization < ActiveRecord::Base 
    belongs_to :user 
    validates :provider, :uid, :presence => true 

    def self.find_or_create(auth_hash) 
    unless auth = find_by_provider_and_uid(auth_hash["provider"], auth_hash["uid"]) 
     user = User.create :name => auth_hash["info"]["name"], :email => auth_hash["info"]["email"] 
     auth = create :user => user, :provider => auth_hash["provider"], :uid => auth_hash["uid"], :token => auth_hash["credentials"]["token"] 
    end 

    auth 
    end 
end 

當我從延長的ActiveRecord :: Base的用戶模型,並嘗試創建身份新註冊我得到這個錯誤:

ActiveRecord::UnknownAttributeError 
unknown attribute: password 

有什麼辦法可以這樣做嗎?我不知道現在該做什麼。

回答

1

嘗試添加attr_accessor:密碼,可能是attr_accessor:電子郵件

3

不知道你仍然有問題,但也許有人對趣聞會。

我只是發表博客上的解決方案,應該可以解決你的問題:

http://bernardi.me/2012/09/using-multiple-omniauth-providers-with-omniauth-identity-on-the-main-user-model/

+0

你有一個顯示在上下文中的教程文件GitHub的倉庫?我無法弄清楚它們是否應該整合到RailsCast的文件中,或者它們是否只是單獨存在。例如,new.html.erb ...哪裏去了?對不起,如果這些都是關注的問題,但我可以使用一些說明。謝謝。 – bergie3000 2013-05-28 05:27:43