0

我有代码

在视野中

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

在控制器中

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

在模型中

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

当我尝试上传头像时 - 没有任何东西到达数据库和文件系统。在日志中我看到

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]

和应用程序呈现图片 /avatars/thumb/missing.png

如何解决?

4

2 回答 2

1

params[:avatar]参数哈希中不存在。你需要使用params[:profile][:avatar]

于 2009-08-26T11:00:51.707 回答
0

我发现,与插件http://github.com/netguru/paperclip-extended/tree/master有冲突。删除此插件可使应用正常工作。

于 2009-08-26T13:43:00.490 回答