2013-12-16 36 views
0

前言與「我是一個新手到紅寶石和編程」條款。紅寶石軌道 - 拉Facebook數據時出現錯誤

我設法讓ruby.railstutorial.org中詳細的登錄系統工作並添加了omniauth。這一切功能,使我可以創建一個用戶。

我現在正試圖從Facebook上拉動更多信息,並設法將位置拉到其他地方。

我遇到了一個問題,當一個Facebook用戶還沒有指定一個位置(我想如果用戶缺乏其他數據,我拉(即性別,或生日),我會有一個類似的問題

我試圖抓住這個錯誤是這樣的:

if auth_hash["extra"]["raw_info"]["location"]["name"] 
     citystate1 = auth_hash["extra"]["raw_info"]["location"]["name"].split(',') 
     else 
     citystate1 =[nil,nil] 
     end 

我得到的錯誤:未定義的方法`[]」的零:NilClass

我的目標是拉任何數據可從Facebook ...如果它不可用,一個零值就足夠了。

user = User.new :firstname => auth_hash["info"]["first_name"], 
         :lastname => auth_hash["info"]["last_name"], 
         :email => auth_hash["info"]["email"].downcase, 
         :dob => Date.strptime(auth_hash["extra"]["raw_info"]["birthday"],'%m/%d/%Y'), 
         :city => citystate1[0], 
         :state => citystate1[1], 

任何幫助,這將不勝感激。謝謝。直到它返回的值或無法找到關鍵

%w(extra raw_info location name).inject{ |k| auth_hash.fetch(k, "there's nothing here") } 

通過鍵陣列這將循環:

回答

0
city, state = auth_hash["extra"]["raw_info"]["location"]["name"] 
    .split(',') rescue [nil, nil] 
+0

這工作完全是更換整個「如果塊」。感謝您的幫助。 – sthoward