【问题标题】:Paperclip cannot create files回形针无法创建文件
【发布时间】:2012-01-17 01:36:42
【问题描述】:

我花了 3 天时间试图解决这个问题:

我正在尝试让用户使用 Paperclip 在我的网站上上传他们的个人资料图片。

一切正常,直到用户单击上传按钮(他能够浏览并选择图片)。单击上传按钮时,Paperclip 不会创建文件(原始图片的原始、小、中和拇指版本),而是出现路由错误并且没有文件夹,没有创建文件,路径中的 :id 字段看起来是空的,并且“回形针的属性”都设置为 NIL...

用户控制器:

def edit
    @user = User.find(params[:id])
    @title = "Upload a profile picture"
  end

  def update
    @user = User.create(params[:user])
    @title = "Update a profile picture"
  end`

意见:

编辑.html.erb:

<h1>
    Ajouter une photo au profil
</h1>

<%= form_for @user,:user, :html => { :multipart => true} do |f| %>
<div class="field">  
  <%= f.label :avatar, "Upload ta photo" %>
  <br />
  <%= f.file_field :avatar %>
</div>

<div class="actions">
    <%= f.submit "Upload" %>
</div>
<% end %>

点击导致更新.html.erb:

<%=  image_tag @user.avatar.url %>
  <%=  image_tag @user.avatar.url(:medium) %>
  <%=  image_tag @user.avatar.url(:thumb) %>

最后但并非最不重要的模型 User.rb:

class User < ActiveRecord::Base
  attr_accessor :password
  attr_accessible :name, :email, :number_of_positive_reco, :confidence_percent, :password,
                  :password_confirmation, :avatar, :avatar_file_name, :avatar_content_file, :avatar_file_size, :avatar_updated_at

  has_attached_file :avatar , :styles => { :medium => "300x300>", :thumb => "100x100>"},
                    :url => "/public/images/:attachment/:id_:style.:extension",
                    :path => ":rails_root/public/images/:attachment/:id_:style.:extension"
                   # :default_url => "/images/Default_profile_picture.png"

  email_regex = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i

  validates :name, :presence => true,
  :length => { :maximum => 20}
  validates :email, :presence => true,
  :format => { :with => email_regex},
  :uniqueness => {:case_sensitive => false}
  validates :password, :presence => true,
  :confirmation => true,
  :length => { :within => 6..40 }
  validates :number_of_positive_reco, :numericality => {:only_integer => true, :greater_than_or_equal_to => 0}
  validates :confidence_percent, :numericality => { :greater_than_or_equal_to => 0.0, :less_than_or_equal_to => 1.0}

  before_save :encrypt_password
  # Return true if the user's password matches the submitted password.
  def has_password?(submitted_password)
    encrypted_password == encrypt(submitted_password)
  end

  def self.authenticate(email, submitted_password)
    user = find_by_email(email)
    return nil if user.nil?
    return user if user.has_password?(submitted_password)
  end

  def self.authenticate_with_salt(id, cookie_salt)
    user = find_by_id(id)
    (user && user.salt == cookie_salt) ? user : nil
  end

  private

  def encrypt_password
    self.salt = make_salt if new_record?
    self.encrypted_password = encrypt(password)
  end

  def encrypt(string)
    secure_hash("#{salt}--#{string}")
  end

  def make_salt
    secure_hash("#{Time.now.utc}--#{password}")
  end

  def secure_hash(string)
    Digest::SHA2.hexdigest(string)
  end

end

这是日志的一部分,你应该看到写在某处的回形针,不是吗?

Processing by UsersController#edit as HTML
  Parameters: {"id"=>"1"}
  User Load (0.7ms)  SELECT "users".* FROM "users" WHERE "users"."id" = 1 LIMIT 1
Rendered layouts/_stylesheets.html.erb (3.6ms)
  User Load (0.9ms)  SELECT "users".* FROM "users" WHERE "users"."id" = 3 LIMIT 1
Rendered layouts/_header.html.erb (7.4ms)
Rendered layouts/_footer.html.erb (2.8ms)
Rendered users/edit.html.erb within layouts/application (55.2ms)
Completed 200 OK in 194ms (Views: 62.5ms | ActiveRecord: 1.6ms)


Started POST "/users/1" for 127.0.0.1 at Fri Dec 09 19:29:19 +0100 2011
  Processing by UsersController#update as HTML
  Parameters: {"commit"=>"Upload", "authenticity_token"=>"c+OuA/dY97fPWukfu4T0nAFEFFg6ty0hK8J3qbGgTJo=", "utf8"=>"���", "id"=>"1", "user"=>{"avatar"=>#<ActionDispatch::Http::UploadedFile:0xb6f31d24 @content_type="image/png", @original_filename="castor.png", @tempfile=#<File:/tmp/RackMultipart20111209-6833-c04i1-0>, @headers="Content-Disposition: form-data; name=\"user[avatar]\"; filename=\"castor.png\"\r\nContent-Type: image/png\r\n">}}
Command :: identify -format %wx%h '/tmp/stream20111209-6833-ms72rm-0.png[0]'
Command :: convert '/tmp/stream20111209-6833-ms72rm-0.png[0]' -resize "100x100>" '/tmp/stream20111209-6833-ms72rm-020111209-6833-ipr2k4-0'
Command :: identify -format %wx%h '/tmp/stream20111209-6833-ms72rm-0.png[0]'
Command :: convert '/tmp/stream20111209-6833-ms72rm-0.png[0]' -resize "300x300>" '/tmp/stream20111209-6833-ms72rm-020111209-6833-1eterfw-0'
  User Load (0.4ms)  SELECT "users"."id" FROM "users" WHERE ("users"."email" IS NULL) LIMIT 1
Rendered layouts/_stylesheets.html.erb (3.1ms)
  User Load (0.7ms)  SELECT "users".* FROM "users" WHERE "users"."id" = 3 LIMIT 1
Rendered layouts/_header.html.erb (5.8ms)
Rendered layouts/_footer.html.erb (2.5ms)
Rendered users/update.html.erb within layouts/application (24.9ms)
Completed 200 OK in 930ms (Views: 31.5ms | ActiveRecord: 1.1ms)


Started GET "/images/avatars/_original.png?1323455359" for 127.0.0.1 at Fri Dec 09 19:29:20 +0100 2011

ActionController::RoutingError (No route matches "/images/avatars/_original.png"):


Rendered /var/lib/gems/1.8/gems/actionpack-3.0.5/lib/action_dispatch/middleware/templates/rescues/routing_error.erb within rescues/layout (2.5ms)


Started GET "/images/avatars/_medium.png?1323455359" for 127.0.0.1 at Fri Dec 09 19:29:20 +0100 2011

ActionController::RoutingError (No route matches "/images/avatars/_medium.png"):


Rendered /var/lib/gems/1.8/gems/actionpack-3.0.5/lib/action_dispatch/middleware/templates/rescues/routing_error.erb within rescues/layout (1.6ms)


Started GET "/images/avatars/_thumb.png?1323455359" for 127.0.0.1 at Fri Dec 09 19:29:20 +0100 2011

ActionController::RoutingError (No route matches "/images/avatars/_thumb.png"):


Rendered /var/lib/gems/1.8/gems/actionpack-3.0.5/lib/action_dispatch/middleware/templates/rescues/routing_error.erb within rescues/layout (1.7ms

所以,一切都是这样的:当点击上传时,Paperclip 无法将文件存储在目录中......

在控制台模式下,我可以毫无问题地设置图片。

在调试模式下,我从来没有看到写过“回形针”,无处可去……(我期待在某处有一个 [Paperclip] 保存附件”……另外,在路由错误的路径中设置了“:id”到“” ...而不是用户的 :id,第 4 个用户为 4 ...但即使使用像“/images/”这样的默认路径,问题仍然存在...

这会让你想起我可能忘记的事情吗?

(ImageMagick工作正常,我在表单中添加了multipart true,普通用户有权创建文件夹)

非常感谢任何提示!

【问题讨论】:

  • 请发布确切的错误消息。
  • @Kristian 完成。感谢您的提问;)
  • 你说你有一个路由错误,请发布。
  • @Kristian:它看起来像这样:Started GET "/images/avatars/_original.png?1323455359" for 127.0.0.1 at Fri Dec 09 19:29:20 +0100 2011 ActionController::RoutingError (No route matches "/images/avatars/_original.png"): 我已经在那里发布了我的模型、视图和控制器:stackoverflow.com/questions/8418310/… 但是 45 人看过这个帖子,但似乎没有人知道 :( ...但我也在上面的原始消息中发布了日志的整个部分
  • 请发布您的操作代码和您的观点。

标签: ruby-on-rails routing paperclip


【解决方案1】:

3 天后发现:由于我使用了密码保护(密码为 attr_accessor),因此无法更新用户,而无需在表单中添加密码字段。

尝试在不输入密码的情况下编辑个人资料图片是行不通的,并且没有生成任何可能让我想到这一点的错误消息。

所以,在编辑视图中,不要忘记在表单中添加密码字段,以便能够更新用户的图片

【讨论】:

    猜你喜欢
    • 2015-06-13
    • 2010-12-31
    • 2018-04-29
    • 1970-01-01
    • 2013-05-25
    • 1970-01-01
    • 2011-10-01
    • 1970-01-01
    • 2018-03-07
    相关资源
    最近更新 更多