使用回形针(本地服务器)上传图像时出错

error uploading images using paperclip (local server)

我在尝试使用回形针 gem 将图像上传到我的(列表)时遇到错误。浏览器输出的错误是:1 error prohibited this listing from being saved: Image has contents that are not what they reported to be **As a note, image magic has been successfully installed on my computer and there are no issues there

我的listing.rb文件

class Listing < ActiveRecord::Base
  has_attached_file :image, :styles => { :medium => "200x", :thumb => "100x100>" }, :default_url => "default.jpg"
  validates_attachment_content_type :image, :content_type => /\Aimage\/.*\Z/
end

我的gem文件

gem "paperclip", "~> 4.3"

我的listings_controller

 def listing_params
      params.require(:listing).permit(:name, :description, :price, :image)
    end
end

最后是我的表格

<%= form_for @listing, :html => { :multipart => true } do |f| %>
...
  ...
  <div class="form-group">
    <%= f.file_field :image, class: "form-control" %>
  </div>
  <div class="form-group">
    <%= f.submit class: "btn btn-primary" %>
  </div>
<% end %>

您尝试附加的文件可能无法正确识别为图像,或者具有图像扩展名且内容不同(例如 PDF)的文件。

此处讨论了一些解决方法:https://github.com/thoughtbot/paperclip/issues/1924

它也可能有助于检查日志文件 - 它应该会告诉您 Paperclip 认为附件的类型是什么。

听起来您需要在系统中包含 file

如果您使用 Windows,您需要从 this URL 下载文件,将其安装到您的硬盘上,然后将其添加到您的 PATH 环境变量:

  1. Click "Start"
  2. On "Computer", right-click and select "Properties"
  3. In Properties, select "Advanced System Settings"
  4. Click the "Environment Variables" button
  5. Locate the "PATH" var - at the end, add the path to your newly installed file.exe (typically C:\Program Files (x86)\GnuWin32\bin)
  6. Restart any CMD shells you have open & see if it works