2010-11-10 8 views

Répondre

3

Ouais, il est en fait assez facile. Il y a le code de mon modèle d'utilisateur utilisant un avatar:

#a part of your model 
    has_attached_file :avatar, :styles => { :thumb => "50x50" }, :storage => :s3, 
         :path => "/:attachment/:id/:style/:filename", 
         :s3_credentials => Rails.root.join("config/s3.yml") 

    attr_accessor :avatar_url 

    before_validation :download_remote_file, :if => :avatar_url_provided? 

    def avatar_url_provided? 
    !self.avatar_url.blank? 
    end 

    def download_remote_file 
    self.avatar = do_download_remote_file 
    end 

    def do_download_remote_file 
    io = open(URI.parse(avatar_url)) 
      def 
       io.original_filename 
       base_uri.path.split('/').last 
      end 
    io.original_filename.blank? ? nil : io 
     rescue Exception => exc 
     logger.debug "ERROR WHEN DOWNLOADING REMOTE AVATAR FOR USER #{self.id} AND REMOTE URL #{self.avatar_url} - ERROR IS #{exc.message}" 
    end 
1

Bien sûr, vous pouvez le faire

+0

logique du programmeur sans faille =) –

Questions connexes