Omniauth Facebook身份验证+身份使用相同的模型,而不是两个
问题描述:
我已经设置了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
有什么办法可以这样做吗?我不知道现在该做什么。
答
尝试添加attr_accessor:密码,可能是attr_accessor:电子邮件
答
不知道你仍然有问题,但也许有人对趣闻会。
我只是发表博客上的解决方案,应该可以解决你的问题:
你有一个显示在上下文中的教程文件GitHub的仓库?我无法弄清楚它们是否应该整合到RailsCast的文件中,或者它们是否只是单独存在。例如,new.html.erb ...哪里去了?对不起,如果这些都是关注的问题,但我可以使用一些说明。谢谢。 – bergie3000 2013-05-28 05:27:43