"NoMethodError in OmniauthCallbacksController#facebook: undefined method `utc' for Mon, 29 Sep 201



在过去的两周里,我一直在利用空闲时间为VoteSquared.org(https://github.com/ForestJay/VoteSquared)。sign_in_and_redirect是发生错误的地方:

class OmniauthCallbacksController < Devise::OmniauthCallbacksController
 def facebook
   # You need to implement the method below in your model (e.g. app/models/user.rb)
   @user = User.from_omniauth(request.env["omniauth.auth"])
   if @user
     sign_in_and_redirect @user, :event => :authentication #this will throw if @user is not activated
     set_flash_message(:notice, :success, :kind => "Facebook") if is_navigational_format?
     #redirect_to politicians_path
   else
     session["devise.facebook_data"] = request.env["omniauth.auth"]
     redirect_to new_user_registration_url
   end
 end
end

我一直在使用https://github.com/plataformatec/devise/wiki/OmniAuth:-概述作为指南(即:这就是我获得此代码的地方)。我知道正在创建用户(通过调试调用)。这是错误堆栈的顶部:

devise (3.3.0) lib/devise/models/confirmable.rb:189:in `confirmation_period_valid?'
devise (3.3.0) lib/devise/models/confirmable.rb:126:in `active_for_authentication?'
devise (3.3.0) lib/devise/hooks/activatable.rb:5:in `block in <top (required)>'
warden (1.2.3) lib/warden/hooks.rb:14:in `call'
warden (1.2.3) lib/warden/hooks.rb:14:in `block in _run_callbacks'
warden (1.2.3) lib/warden/hooks.rb:9:in `each'
warden (1.2.3) lib/warden/hooks.rb:9:in `_run_callbacks'
warden (1.2.3) lib/warden/manager.rb:53:in `_run_callbacks'
warden (1.2.3) lib/warden/proxy.rb:179:in `set_user'
devise (3.3.0) lib/devise/controllers/sign_in_out.rb:43:in `sign_in'

我在谷歌上发现的最相关的错误是https://github.com/plataformatec/devise/issues/3078。我知道Devise不推荐给新的Rails开发人员,但Facebook是这个应用程序的必备工具。我曾短暂尝试过的一个解决方案是重定向到另一个页面,因为我知道登录已经发生,并且用户已经创建。我解决的问题是,似乎没有全局变量可以用来访问User(它与Rails全局变量有关)。欢迎提出任何建议。在这一点上,我真的只需要在登录后访问用户信息,这样我就知道他们已经登录,并且可以将他们的用户与其他对象关联起来。

查看Devise代码,我意识到问题所在的字段是User::confirmed_at。以下是我如何在user.rb:中定义该字段

  key :confirmed_at, Time

这是一个破解的解决方案,但它似乎让我度过了这次崩溃(又一次崩溃)。我将上面的代码更改为以下代码:

class OmniauthCallbacksController < Devise::OmniauthCallbacksController
  def facebook
    # You need to implement the method below in your model (e.g. app/models/user.rb)
    @user = User.from_omniauth(request.env["omniauth.auth"])
    if @user
      # The following line forces confirmed_at to be set so it doesn't crash in Devise's type conversion
      @user.confirmed_at = Time.now.utc
      sign_in_and_redirect @user, :event => :authentication #this will throw if @user is not activated
      set_flash_message(:notice, :success, :kind => "Facebook") if is_navigational_format?
      #redirect_to politicians_path
    else
      session["devise.facebook_data"] = request.env["omniauth.auth"]
      redirect_to new_user_registration_url
    end
  end
end

我现在将使用这个解决方案,但我喜欢一个不必手动设置值的解决方案。我认为最好让迪维斯来做那项工作。

最新更新