【发布时间】:2013-01-05 18:38:39
【问题描述】:
我是 Rails 的新手。
所以...在我的 Rails 应用程序中,我有 OmniAuth Facebook 集成,我想在我的数据库中添加一些字段,例如名字、姓氏和位置。
我关注了这个wiki,我有一个简单的登录,但没有额外的字段(名字、姓氏、位置)。
所以,我在我的 config/initializers/devise.rb 中添加了这个:
require 'omniauth-facebook'
config.omniauth :facebook, '123456...', '123456...',
scope: 'first_name, last_name, location',
stategy_class: OmniAuth::Strategies::Facebook
所以,如果我是正确的,上面会要求这些额外的字段。
现在,在我的模型 user.rb 中,我想添加 3 行,它将请求的值传递给数据库。
def self.find_for_facebook_omniauth(omniauth, signed_in_resource=nil)
basic = {
provider: omniauth.provider,
uid: omniauth.uid,
}
User.where(basic).first || User.create(basic.merge(
firstname: omniauth.info.firstname, # these are the
lastname: omniauth.info.lastname, # lines I'm not
location: omniauth.info.location, # sure of
email: omniauth.info.email,
password: Devise.friendly_token[0,20],
))
end
【问题讨论】:
-
看起来不错。你试过了吗?
-
@Baldrick 是的,控制器将我重定向到
/users/sign_in视图 -
config_omniauth语句中的:scope参数不正确。:strategy_class也缺少一个“r”。你可以试试类似的东西:config.omniauth :facebook, "123456...", "123456...", :scope => "email, user_location, read_stream", :strategy_class => OmniAuth::Strategies::Facebook -
@mccannf 感谢您的帮助。服务器实际上需要重新启动,并且字段是
omniauth.info.first_name也很简单:scope => 'location'工作正常
标签: ruby-on-rails facebook ruby-on-rails-3 devise omniauth