2009-08-26 7 views
0

J'ai le codePaperclip, Rails 2.3.3 et Mac OS X

en vue

<% uberform_for :profile, :html => { :multipart => true, :method => :put }, :url => update_avatar_path do |f| %> 
    <%= f.file_field :avatar %> 

    <p><%= f.submit 'Upload avatar' %></p> 
<% end %> 

dans le contrôleur

def update_avatar 
    current_user.profile.update_attribute(:avatar, params[:avatar]) 
    redirect_to user_path(current_user) 
    end 

et dans le modèle

class Profile < ActiveRecord::Base 
    attr_accessible :first_name, :last_name, :nickname 
    has_attached_file :avatar, :styles => {:thumb => '100x100>'}, 
    :path => '#{RAILS_ROOT}/public/images/avatars/:id/:normalized_basename_:style.:extension', 
    :url => '/images/avatars/:id/:normalized_basename_:style.:extension' 

    validates_attachment_content_type :avatar, :content_type => ['image/jpeg', 'image/png', 'image/gif'] 
    validates_attachment_size :avatar, :less_than => 1.megabytes 
end 

Et quand i » m essayant de télécharger avatar - rien n'arrive à DB et à la système de fichiers. Dans les journaux que je vois

Processing UsersController#update_avatar (for 127.0.0.1 at 2009-08-26 11:09:04) [PUT] 
    Parameters: {"profile"=>{"avatar"=>#<File:/var/folders/zg/zghNxzjrFP02se1nq1fKQ++++TI/-Tmp-/RackMultipart20090826-3425-1akehpx-0>}, "authenticity_token"=>"Frf1ozk01ePIhvsPSX3k1ophgvHHrnBFKhFcF21co+o="} 
    User Load (0.5ms) SELECT * FROM "users" WHERE ("users"."id" = 1) LIMIT 1 
    Profile Load (0.4ms) SELECT * FROM "profiles" WHERE ("profiles".user_id = 1) LIMIT 1 
[paperclip] Saving attachments. 
Redirected to http://localhost:3000/users/alec-c4 
Completed in 18ms (DB: 1) | 302 Found [http://localhost/update_avatar] 

et l'application rend l'image /avatars/thumb/missing.png

Comment résoudre?

Répondre

1

params[:avatar] n'est pas présent dans le hachage des paramètres. Vous aurez besoin d'utiliser params[:profile][:avatar]

+0

ArgumentError dans UsersController # update_avatar nombre incorrect d'arguments (2 pour 1) et dans la console >> p = Profile.find 1 ...... >> p.avatar = File.open ('/ Users/alec/Documents/Données personnelles/avatars/11412573.jpg') ArgumentError: nombre incorrect d'arguments (2 pour 1) –