2012-12-23 50 views
0

我的應用使用carrierwave和omniauth(臉書),我有一個小問題。Rails] Carrierwave With OmniAuth

  1. 當用戶首次使用Facebook帳戶登錄時,我將他的Facebook個人資料圖像作爲其默認圖像。 (他不必註冊FB賬戶或任何東西,當他登錄時,他已經註冊了我的應用程序,但沒有提供任何更多信息)
  2. 當用戶第一次從我的應用創建一個「本地」帳戶時,他可以選擇上傳個人資料圖片。因此,用戶模型中有一個profile_image字段,並且來自carrierwave的ProfileUploader已安裝在此字段中。

問題:當我將facebook圖片網址保存到這個字段時,它給了我錯誤。我認爲這是因爲有一個上傳器安裝到這個字段,雖然該字段是字符串類型,我不能只保存一個URL到這個領域,而不通過carrierwave上傳圖像。

我通過將FB圖像URL存儲到不同的字段other_profile_image來解決此問題,並且每當從FB創建用戶帳戶時,我都會從該字段中調用其圖像。

例如,

<% if @user.provider == "local" %> 
     <% if @user.profile_image.blank? %> 
      <%= link_to image_tag('default.jpg'), '/users/'[email protected]_s %> 
     <% else %> 
      <%= link_to image_tag(@user.profile_image_url(:thumb)), '/users/'[email protected]_s %> 
     <% end %> 
<% elsif @user.provider == "facebook" %> 
     <%= link_to image_tag(@user.other_profile_image), '/users/'[email protected]_s %> 
<% end %> 

provider=local specifies accounts created on the application, not from other third-parties like FB

我的變通辦法工作,但它的煩我,因爲我知道必須有一個更好的解決辦法來處理這個問題。我需要找出如何處理以下幾個簡單的任務:

  1. 當用戶註冊本地,他可以選擇上傳檔案圖片。如果他沒有,我想將我的默認圖片的網址存儲到他的個人資料圖片網址字段。
  2. 當用戶使用他的FB賬戶時,他的臉書圖片應該被設置爲他的初始檔案圖片。現在,我使用了兩個不同的字段,profile_imageother_profile_image,因爲carrierwave在我嘗試將URL字符串存儲到profile_image字段中時拋出了錯誤,其中載入了carrierwave上載程序。

我希望有一個很好的方法來解決這個問題!我很感激任何幫助。非常感謝你。

回答

3

您可以從遠程網址上傳照片。

class User < ActiveRecord::Base 
    attr_accessible :profile_image, :remote_profile_image_url 

    mount_uploader :profile_image, ProfileUploader 
end 


# somethere 
user.remote_profile_image_url = "http://example.com/userpic.png" 
user.save 
+0

完全忘了這件事。謝謝!! –