views:

615

answers:

2

Hi, I have code

in view

<% 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 %>

in controller

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

and in model

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

And when i'm trying to upload avatar - nothing arrives to db and to the filesystem. In Logs i see

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]

and application renders picture /avatars/thumb/missing.png

How to solve it?

+1  A: 

params[:avatar] is not present in the params hash. You'll need to use params[:profile][:avatar]

Olly
ArgumentError in UsersController#update_avatarwrong number of arguments (2 for 1)and in console>> p= Profile.find 1......>> p.avatar = File.open('/Users/alec/Documents/Personal Data/avatars/11412573.jpg')ArgumentError: wrong number of arguments (2 for 1)
Alexey Poimtsev
A: 

I've found, that there are conflict with plugin http://github.com/netguru/paperclip-extended/tree/master. Removing this plugin makes app work fine.

Alexey Poimtsev