我遵循了 Ryan Bates #235 设计和 OmniAuth(修订版)http://railscasts.com/episodes/235-devise-and-omniauth-revised并且能够使其工作。
现在,我想向用户添加一个配置文件。
所以,我尝试了这个...
def self.from_omniauth(auth)
where(auth.slice(:provider, :uid)).first_or_create do |user|
user.provider = auth.provider
user.uid = auth.uid
user.username = auth.info.nickname
user.profile = user.build_profile
user.profile.name = auth.info.name
user.profile.save
end
end
乍一看,一切似乎都很好。但我注意到它没有更新“名称”属性。
1.9.3p125 :019 > Profile.last
Profile Load (0.4ms) SELECT "profiles".* FROM "profiles" ORDER BY "profiles"."id" DESC LIMIT 1
=> #<Profile id: 8, user_id: 3, name: nil, created_at: "2012-08-18 06:00:59", updated_at: "2012-08-18 06:00:59">
所以我试图设置一个值,这就是发生的事情:
1.9.3p125 :020 > p=Profile.last
Profile Load (0.4ms) SELECT "profiles".* FROM "profiles" ORDER BY "profiles"."id" DESC LIMIT 1
=> #<Profile id: 8, user_id: 3, name: nil, created_at: "2012-08-18 06:00:59", updated_at: "2012-08-18 06:00:59">
1.9.3p125 :021 > p.name = "Adam"
=> "Adam"
1.9.3p125 :022 > p.save
(0.1ms) BEGIN
(0.1ms) COMMIT
=> true
1.9.3p125 :023 > p.name
=> "Adam"
1.9.3p125 :024 > p
=> #<Profile id: 8, user_id: 3, name: nil, created_at: "2012-08-18 06:00:59", updated_at: "2012-08-18 06:00:59">
我期待名称会被更新。我应该怎么做?
谢谢