2013-04-14 32 views
0

我在我的用戶表中有一列名爲「圖像」每次用戶重新登錄時如何更新表屬性? (導軌3 + omniauth)

此圖像存儲從用戶的Twitter帳戶拉出的化身的URL。

部分用戶表的

create_table "users", :force => true do |t| 
    t.string "image" 
    end 

我使用omniauth允許用戶使用Twitter,deauthenticate,重新驗證身份驗證等

他們deauthenticate,並嘗試重新進行身份驗證時,當我想驗證後該頭像在我的網站上匹配。下面是我使用的代碼:

class AuthenticationsController < InheritedResources::Base 
def create 
    omniauth = request.env['omniauth.auth'] 
    authentication = Authentication.find_by_provider_and_uid(omniauth['provider'], omniauth['uid']) 
    if authentication 
    user = User.find(authentication.user_id) 
    sign_in_and_redirect user 
    else current_user 
    token = omniauth['credentials']['token'] 
    secret = omniauth['credentials']['secret'] 
    current_user.authentications.create!(:provider => omniauth['provider'], :uid => omniauth['uid'], :token => token, :secret => secret) 
    current_user.update_attribute(:image => omniauth['info']['image']) 
    flash[:success] = "Authentication successful" 
    sign_in_and_redirect current_user 
    end 
end 

它基本上是這條線,當他們試圖重新驗證

current_user.update_attribute(:image => omniauth['info']['image']) 

但我得到的錯誤。上面這個特定的代碼給了我wrong number of arguments (1 for 2)。 我嘗試了其他方法,但我得到了undefined method update_attribute。任何人都知道我可以如何使用twitter + omniauth的數據更新用戶表中的「圖片」列?

回答

0

看的update_attribute語法,它需要兩個參數namevalue

current_user.update_attribute(:image, omniauth['info']['image']) 

Futhermore,我敢肯定,在Twitter上的頭像URL永遠不會改變:它總是顯示用戶當前的頭像。因此,您無需在數據庫中更新它,只需在您的頁面中直接使用它即可獲取最新用戶的頭像。