我根据本教程设置了Omniauth Facebook身份验证:http://net.tutsplus.com/tutorials/ruby/how-to-use-omniauth-to-authenticate-your-users/ 现在我正在尝试将它与omniauth-identity结合使用相同的用户模型而不是本教程中的单独身份模型:http://railscasts.com/episodes/304-omniauth-identity?view=asciicast,但我无法使其正常工作。
这是我的初始化器/ omniauth.rb文件:
Rails.application.config.middleware.use OmniAuth::Builder do
provider :facebook, 'xxxxx', 'xxxxx'
provider :identity, :fields => [:email], :model => User
end
我已将omniauth-identity所需的'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
但是当我这样做时,授权模型中创建用户和授权模型的代码无法正常工作 当User模型从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扩展User模型并尝试使用Identity创建新注册时,我收到此错误:
ActiveRecord::UnknownAttributeError
unknown attribute: password
有没有办法以这种方式工作?我现在不知道该怎么做。
答案 0 :(得分:3)
不确定你是否仍然遇到这个问题,但也许是interwebz上的某个人会。
我刚刚通过博客发布了一个解决方案,应该可以解决您的问题:
答案 1 :(得分:1)
尝试添加attr_accessor:密码,可能是attr_accessor:email