mgh*_*mgh 2 ruby-on-rails paperclip
我使用"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
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)
但是当我想要保存我上传的图片时,我得到以下错误:
1 error prohibited this product from being saved:
Picture has an extension that does not match its contents
Run Code Online (Sandbox Code Playgroud)
系统日志:
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)
Run Code Online (Sandbox Code Playgroud)
问题出在哪儿?
如何将pdf文件保存到paperclip视图中并显示?
小智 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
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
886 次 |
| 最近记录: |