我在我的 rails 应用程序中使用 devise omniauth,这里是 User 类
class User < ActiveRecord::Base
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable, :omniauthable
attr_accessible :email, :password, :password_confirmation, :remember_me, :encrypted_password, :fb_id
def set_facebook_info(info)
@facebook_info = info
end
def get_facebook_info
@facebook_info
end
def self.find_for_facebook_oauth(access_token, signed_in_resource=nil)
data = access_token.extra.raw_info
if user = User.find_by_email(data.email)
if not user.fb_id
user.fb_id = access_token.uid
user.save
end
user.set_facebook_info "whatever" <-- I tried here
user
else # Create a user with a stub password.
user = User.create(:email => data.email, :password => Devise.friendly_token[0,20], :fb_id => access_token.uid)
user
end
end
def self.new_with_session(params, session)
super.tap do |user|
user.set_facebook_info "whatever" # <-- I Tried here too
if data = session["devise.facebook_data"] && session["devise.facebook_data"]["extra"]["user_hash"]
user.email = data["email"]
end
end
end
end
我想使用 set 和 get 方法来保存一些 facebook 用户信息。但是当我在视图中使用 current_user 时,它没有给我在我的 User 类中设置的值。就像在 application.html.erb 中一样:
<span><%= current_user.get_facebook_info %></span>
返回一个空值
有人知道吗?这应该是一个常见的情况。一般来说,我们如何通过设计将一些未连接到数据库的属性分配给 current_user ?
谢谢