2014-10-02 21 views
0

我在過去的兩週中花了我的空閒時間嘗試讓VoteSquared的Facebook身份驗證工作.org(https://github.com/ForestJay/VoteSquared)。該sign_in_and_redirect是在錯誤發生:「OmniauthCallbacksController中的NoMethodError#Facebook:2014年9月29日,星期一,使用Rails和MongoMapper的Devise的未定義方法`utc'

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:-Overview爲指導(即:這就是我得到這個代碼)。我知道用戶正在創建(通過調試調用)。以下是錯誤堆棧的頂部:

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是這個應用程序的必需品。我簡要嘗試過的一種解決方法是重新定向到另一個頁面,因爲我知道登錄已經發生並且用戶已創建。我的解決方法的問題是似乎沒有可用於訪問用戶的全局變量(與Rails global variable有關)。任何建議表示讚賞。此時,我確實需要在登錄後訪問用戶信息,以便我知道他們已登錄並可將其用戶關聯到其他對象。

看着設計代碼,我意識到問題領域是User :: confirmed_at。這裏是我在user.rb定義字段:

key :confirmed_at, Time 

回答

1

這是一個解決方案的黑客攻擊,但它似乎讓我過去,這崩潰(另一個崩潰)。我改變了上面的代碼如下:

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 

我將使用這個解決方案了,但我喜歡一個解決方案,我沒有手動設置的值。我認爲最好讓Devise做這件事。

相關問題