【问题标题】:Paperclip, Rails 2.3.3 and OSX回形针、Rails 2.3.3 和 OSX
【发布时间】:2009-08-26 07:11:35
【问题描述】:

我有代码

在视图中

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

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

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

如何解决?

【问题讨论】:

    标签: ruby-on-rails ruby macos paperclip


    【解决方案1】:

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

    【讨论】:

    • UserController#update_avatar 中的ArgumentError 参数数量错误(2 代表 1)和控制台 >> p= Profile.find 1 ...... >> p.avatar = File.open(' /Users/alec/Documents/Personal Data/avatars/11412573.jpg') ArgumentError: 参数数量错误(2 比 1)
    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 2013-07-25
      • 1970-01-01
      • 1970-01-01
      • 2011-06-22
      • 2012-11-17
      • 2015-11-14
      • 1970-01-01
      • 2013-05-15
      • 1970-01-01
      相关资源
      最近更新 更多