我使用" paperclip","〜> 4.1" (在Windows 8上)将图片保存到我的产品中。 我有以下代码:
products_controller:
class ProductsController < ApplicationController
before_action :set_product, only: [:show, :edit, :update, :destroy]
def index
@products = Product.all
end
def show
end
def new
@product = Product.new
end
def edit
@product = Product.all
end
def create
@product = Product.new(product_params)
respond_to do |format|
if @product.save
format.html { redirect_to @product, notice: 'Product was successfully created.' }
format.json { render action: 'show', status: :created, location: @product }
else
format.html {
render action: 'new'
}
format.json { render json: @product.errors, status: :unprocessable_entity }
end
end
end
def update
respond_to do |format|
if @product.update(product_params)
format.html { redirect_to @product, notice: 'Product was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: 'edit' }
format.json { render json: @product.errors, status: :unprocessable_entity }
end
end
end
def destroy
@product.destroy
respond_to do |format|
format.html { redirect_to products_url }
format.json { head :no_content }
end
end
private
def set_product
@product = Product.find(params[:id])
end
def product_params
params.require(:product).permit(:name, :code,:picture)
end
end
product.rb
class Product < ActiveRecord::Base
has_attached_file :picture, :styles => { :medium => "300x300>", :thumb => "100x100>" }, :default_url => "/images/missing.png"
validates_attachment_content_type :picture, :content_type => [ 'image/gif', 'image/png', 'image/x-png', 'image/jpeg', 'image/pjpeg', 'image/jpg' ]
end
但是当我想要保存我上传的图片时,我会收到以下错误:
1 error prohibited this product from being saved:
Picture has an extension that does not match its contents
系统日志:
Started POST "/products" for 127.0.0.1 at 2014-05-04 23:23:31 +0430
Processing by ProductsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"XVKgrUjhmB6auJXM5zTrDFrw7GLS0jdUPFelXt4se4Y=", "product"=>{"name"=>"Product 453", "code"=>"324324", "category_id"=>"10", "describe"=>"", "picture"=>#<ActionDispatch::Http::UploadedFile:0x00000004fdb750 @tempfile=#<Tempfile:C:/Users/MGH~1.119/AppData/Local/Temp/RackMultipart20140504-5856-p42svx>, @original_filename="pic.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"product[picture]\"; filename=\"pic.jpg\"\r\nContent-Type: image/jpeg\r\n">}, "commit"=>"Save change"}
User Load (1.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = 17 ORDER BY "users"."id" ASC LIMIT 1
Command :: file -b --mime-type "C:/Users/MGH~1.119/AppData/Local/Temp/be0933fb4765581454c720c6cac4755920140504-5856-15ak6qf"
[paperclip] Content Type Spoof: Filename pic.jpg (["image/jpeg"]), content type discovered from file command: . See documentation to allow this combination.
Responsibility Load (0.0ms) SELECT "responsibilities".* FROM "responsibilities" WHERE "responsibilities"."user_id" = ? ORDER BY "responsibilities"."id" ASC LIMIT 1 [["user_id", 17]]
(1.0ms) begin transaction
Command :: file -b --mime-type "C:/Users/MGH~1.119/AppData/Local/Temp/6db10b0624aa4c70237ca9e622c03f4620140504-5856-t1i2gw"
[paperclip] Content Type Spoof: Filename pic.jpg (["image/jpeg"]), content type discovered from file command: . See documentation to allow this combination.
(0.0ms) rollback transaction
Category Load (0.0ms) SELECT "categories".* FROM "categories"
Rendered products/_form.html.erb (2.0ms)
Rendered products/new.html.erb within layouts/product (3.0ms)
Rendered layouts/_header.html.erb (0.0ms)
Rendered layouts/_sidebar.erb (0.0ms)
Completed 200 OK in 159ms (Views: 16.0ms | ActiveRecord: 2.0ms)
问题出在哪里?
如何将pdf
个文件保存到paperclip
并在视图中显示?
答案 0 :(得分:7)
Paperclip在v4.0中引入了欺骗验证,以确保文件内容与扩展名匹配。
它使用file
命令来确定文件的MIME类型,如果你使用linux或OS X,那就没问题了。不幸的是,Windows没有file
命令所以它回来了空白 - 因为根据其扩展名,它与文件的预期MIME类型不匹配,您会收到欺骗错误。
你的日志中的这一点:
content type discovered from file command: .
冒号和句号之间的位是file
命令的输出;在Windows上总是空白。
我知道Windows的最佳解决方法是禁用欺骗。为此,你需要在初始化器中加入这样的东西:
module Paperclip
class MediaTypeSpoofDetector
def spoofed?
false
end
end
end
答案 1 :(得分:0)
我尝试了user740584提出的解决方案,但它并没有为我解决问题。我在intializers文件夹中创建了一个单独的文件,名为&#34; Disable_Spoofing.rb&#34;并将建议的代码放在那里。
为我解决这个问题的方法是降级我的回形针版本。我从4.2.1降级到3.5.3。然后我跑了#34; $ bundle install&#34;然后重新启动rails服务器,然后我就可以成功上传图像。