从omniauth哈希中提取电子邮件以验证用户模型

发布于 2024-10-24 23:32:15 字数 803 浏览 5 评论 0原文

我正在使用omniauth并设计让用户使用他们的facebook帐户登录,一切正常,但是当我尝试从哈希中提取他们的电子邮件时,我收到此错误:

NoMethodError in AuthenticationsController#create
undefined method `id' for "/":String

这是错误的完整日志:http://pastie.org/1698569

错误消失了,刷新后我就可以正常登录了!

编辑:事实证明,它在我的身份验证控制器中的第 22 行

     sign_in_and_redirect_to(:user, root_path) 

出于某种原因,运行此方法后我无法登录 :user

 def apply_facebook(omniauth)                    
   if (extra = omniauth['extra']['user_hash'] rescue false)  
   self.email = (extra['email'] rescue '')
 end
end

但是,如果我不运行该方法,那么它可以登录并重定向到就好

这是我的控制器/模型http://pastie.org/1698453

非常感谢任何帮助

I'm using omniauth and devise to log users in with their facebook acounts, everything works however when I try to pull their email out of the hash I get this error:

NoMethodError in AuthenticationsController#create
undefined method `id' for "/":String

Here's the full log of the error : http://pastie.org/1698569

The error goes away and it lets me log in just fine after I refresh!

EDIT: It turns out that its line 22 in my authentications controller

     sign_in_and_redirect_to(:user, root_path) 

For some reason after running this method I can't sign_in the :user

 def apply_facebook(omniauth)                    
   if (extra = omniauth['extra']['user_hash'] rescue false)  
   self.email = (extra['email'] rescue '')
 end
end

However, if I don't run that method, then it can sign_in_and_redirect_to just fine

Here's my controllers/model http://pastie.org/1698453

Really appreciate any help

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

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

发布评论

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

评论(1

罗罗贝儿 2024-10-31 23:32:15

您不能使用 root_path 作为 sign_in_and_redirect 的第二个参数。以下是您可以使用它的一些可用方法:

sign_in_and_redirect :user, @user                      # sign_in(scope, resource)
sign_in_and_redirect @user                             # sign_in(resource)
sign_in_and_redirect @user, :event => :authentication  # sign_in(resource, options)
sign_in_and_redirect @user, :bypass => true            # sign_in(resource, options)

由于您的第二个参数既不是资源也不是选项(它是一个字符串),因此您会收到错误。您需要将其更改为:

sign_in_and_redirect(:user, user) # based on your pastie

如果您想自定义返回路径以强制其在登录后转到不同的 URL,您可以在 ApplicationController 中执行以下操作:

def after_sign_in_path_for(resource)
  "/go/to/this/path"
end

You can't use root_path as the second parameter for sign_in_and_redirect . Here are some available ways you can use it:

sign_in_and_redirect :user, @user                      # sign_in(scope, resource)
sign_in_and_redirect @user                             # sign_in(resource)
sign_in_and_redirect @user, :event => :authentication  # sign_in(resource, options)
sign_in_and_redirect @user, :bypass => true            # sign_in(resource, options)

Since your second parameter isn't either a resource or options (it's a string), you're getting an error. You need to change it to:

sign_in_and_redirect(:user, user) # based on your pastie

If you want to customize the return path to force it to go to a different URL after sign-in, you can do something like this in your ApplicationController:

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