I use "paperclip", "~> 4.1" (on windows 8) to save picture to my product.
I have below code:
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
but when I want save the picture that I upload, I get below error:
1 error prohibited this product from being saved:
Picture has an extension that does not match its contents
Log of system:
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)
Where is the problem?
And how can I save pdf
file to paperclip
and show it in view?