我已經花了最後一天的時間來解決這個問題,這使我瘋了。Rails devise omniauth-facebook .persisted?返回false
昨晚,我在我的網站上登錄了Facebook登錄,並且正在檢索基本的用戶信息。當我加入:scope => 'user_birthday'
到config/initializers/omniauth.rb
現在看起來是這樣的
Rails.application.config.middleware.use OmniAuth::Builder do
provider :facebook, "APP_ID", "APP_SECRET", :scope => 'user_birthday'
end
僅供參考,我從config/initializers/devise.rb
刪除線config.omniauth :facebook, "APP_ID", "APP_SECRET"
我的問題開始我花了幾個小時試圖讓這個工作,但不得不放棄最終。然後今天早上我再次運行它,它工作。喜出望外,我試圖給:scope
添加另一個參數,但現在整個事情又被打破了。如果我刪除了:scope
,但每當我把它重新放入它時(即使它只是:scope => 'user_birthday'
,就像我今天早上開始工作的第一件東西),我可以使它工作。
要查找的問題,我把調試代碼omniauth_callbacks_controller.rb
,它現在的樣子:
class Users::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"])
puts "start before persist debug"
puts @user.birthday
puts @user.persisted?
puts "end before persist debug"
if @user.persisted?
puts "Start persisted debug"
puts request.env["omniauth.auth"]
puts "End debug"
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?
else
session["devise.facebook_data"] = request.env["omniauth.auth"]
puts "Start unpersisted debug"
puts request.env["omniauth.auth"]
puts "End debug"
redirect_to new_user_registration_url
end
end
end
該調試清楚地表明,Facebook正在返回所需的信息,但該應用程序失敗,因爲.persisted?
是返回false和所以我得到重新定向到返回下面的new_user_registration頁: -
NoMethodError in Devise::Registrations#new
Showing /home/action/workspace/cloudapp/app/views/devise/shared/_links.html.erb where line #23 raised:
undefined method `omniauth_authorize_path' for #<#<Class:0x007f3aeffff038>:0x007f3aefffdf08>
我不能爲我的生命弄清楚爲什麼.persisted?
是返回false。我用Heroku postgresql數據庫使用Nitrous.io進行開發。我已經證實有數據庫中沒有用戶通過運行
rails c
User.all
這將返回:
User Load (89.4ms) SELECT "users".* FROM "users"
=> #<ActiveRecord::Relation []>
我有一種感覺,問題是出在models/user.rb
但我無法弄清楚如何調試它看看它是否找到了一個用戶,因此不會堅持或試圖創建一個和失敗。有誰知道一個簡單的方法來調試呢?
def self.from_omniauth(auth)
where(provider: auth.provider, uid: auth.uid).first_or_create do |user|
user.email = auth.info.email
user.password = Devise.friendly_token[0,20]
user.name = auth.info.name # assuming the user model has a name
user.birthday = auth.extra.raw_info.birthday
# user.image = auth.info.image # assuming the user model has an image
end
end
我已經完成了大約50次,接近放棄。
我能想到的唯一的事情是where(provider: auth.provider, uid: auth.uid)
返回的東西(這不應該是因爲我的數據庫是空的)。是否可能有一個索引存在於我的數據庫之外的某個地方,這就是它正在搜索的內容?
請爲我的理智,任何人都可以幫忙嗎?如果您需要更多的信息,我會很樂意提供它
Edit 1
剛試過以下,它的作品,這使得我更加糊塗比以往任何時候:
- 從我的Facebook帳戶刪除該應用程序爲我使用該帳戶進行測試
- 嘗試使用臉書登錄並使用
:scope => 'user_birthday'
離開。Facebook列出了所需的權限爲your public profile and birthday
。接受並返回到我的網站,失敗按上述(即使信息肯定被髮回) - 刪除
:scope => 'user_birthday'
並嘗試再次登錄Facebook使用。定向到Facebook,其中列出了要求爲your public profile and email address
的權限。接受並獲得指導回到現在有效的網站,並且還有用戶的生日存儲和訪問,因爲我從上面的第2號的facebook獲得了許可。
我完全不知所措現在
什麼是你的生日列的類型(用戶表)? – mohameddiaa27 2014-11-03 00:19:28
字符串。根據我的編輯,它是保存和訪問步驟3 – 2014-11-03 00:24:07