Onmiauth-Facebook 未将 facebook_id 保存到用户表中

发布于 2024-12-21 01:44:02 字数 1275 浏览 3 评论 0原文

我已经运行了bundle exec rake db:migrate并且有2个数据库。我也在开发中使用 SQLite。 当我访问 http://localhost:3000/auth/facebook 时出现以下错误:

当你没有预料到的时候,你得到了一个 nil 对象!您可能期望一个 Array 的实例。评估 nil 时发生错误。[]

它指向 app/controllers/sessions_controller.rb:12:in `create' 中的这段代码 :

    def create
      auth_hash = request.env['omniauth.auth']

      @authorization = Authorization.find_by_provider_and_uid(auth_hash["provider"], auth_hash["uid"])
      if @authorization
        render :text => "Welcome back #{@authorization.user.name}! You have already signed up."
      else
######line 12->  user = User.new :name => auth_hash["user_info"]["name"], :email => auth_hash["user_info"]["email"]
        user.authorizations.build :provider => auth_hash["provider"], :uid => auth_hash["uid"]
        user.save

        render :text => "Hi #{user.name}! You've signed up."
      end
    end

我认为这意味着我的信息数组是空的,并且将我的范围缩小到数据没有保存到我的数据库中的推理范围。

顺便说一句,我正在执行以下步骤 6 http://net.tutsplus.com/tutorials/ruby/how-to-use-omniauth-to-authenticate-your-users/

I've ran bundle exec rake db:migrate and have 2 databases. I'm also using SQLite in development.
I get the following error when I go to http://localhost:3000/auth/facebook:

You have a nil object when you didn't expect it! You might have expected an instance of Array. The error occurred while evaluating nil.[]

It points to this code in app/controllers/sessions_controller.rb:12:in `create'
:

    def create
      auth_hash = request.env['omniauth.auth']

      @authorization = Authorization.find_by_provider_and_uid(auth_hash["provider"], auth_hash["uid"])
      if @authorization
        render :text => "Welcome back #{@authorization.user.name}! You have already signed up."
      else
######line 12->  user = User.new :name => auth_hash["user_info"]["name"], :email => auth_hash["user_info"]["email"]
        user.authorizations.build :provider => auth_hash["provider"], :uid => auth_hash["uid"]
        user.save

        render :text => "Hi #{user.name}! You've signed up."
      end
    end

I think this means that my array of info is empty, and narrows me down to this reasoning that data is not being saved onto my database.

Btw, I'm on step 6 following http://net.tutsplus.com/tutorials/ruby/how-to-use-omniauth-to-authenticate-your-users/

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

想你只要分分秒秒 2024-12-28 01:44:02

好的。我想我明白这个例子哪里出了问题。上述站点中使用的示例使用的是先前版本的omniauth,它使用“user_info”作为哈希名称。新的 1.0 版本的omniauth 将该名称更改为“info”,并将提供者策略拆分为单独的 gem,因此您必须在 Gemfile 中单独指定它们(例如:omniauth-facebook、omniauth-twitter 等)。因此,您不再使用 Gemfile 中简单的“omniauth”gem。

https://github.com/intridea/omniauth/wiki/Upgrading-to-1.0

Ok. I think I see where this example went awry. The example that is being used in the above site is using the previous version of omniauth, which used "user_info" as the name of the hash. The new 1.0 version of omniauth changed that name to just "info" and split the provider strategies into separate gems, so you have to specify them separately in your Gemfile (ex: omniauth-facebook, omniauth-twitter, etc). So you don't use the just plain 'omniauth' gem in the Gemfile anymore.

https://github.com/intridea/omniauth/wiki/Upgrading-to-1.0

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文