Paperclip Image not being added in mysql database

走远了吗. 提交于 2019-12-24 12:23:23

问题


I am trying to insert image in database using paperclip gem 4 but i am getting the following errors in my rails console:

  Processing by ArticlesController#create as HTML

  Parameters: {"utf8"=>"✓", 
 "authenticity_token"=>"chZ3Zjs0OrcRirp7SNr8PhvuIenX2itoM8GzyUhSBrk=", "article"=>
  {"headline"=>"dlvmlmvc., c", "content"=>"kdfl;d,av,v',", "photo"=>#
  <ActionDispatch::Http::UploadedFile:0x007ffb091f2c08 @original_filename="bigb.jpg", 
   @content_type="image/jpeg", @headers="Content-Disposition: form-data; 
   name=\"article[photo]\"; filename=\"bigb.jpg\"\r\nContent-Type: image/jpeg\r\n",
  @tempfile=#<Tempfile:/tmp/RackMultipart20140711-4464-p7ou5q>>}, "commit"=>"Create",
  "category_id"=>"1"}

  Category Load (0.1ms)  SELECT "categories".* FROM "categories" WHERE 
 "categories"."id" = ? LIMIT 1  [["id", "1"]]

Command :: file -b --mime '/tmp/8528c4a1ed4aba84a53e4db3c799d5e320140711-4464-1jdkmqc.jpg'

 (0.1ms)  begin transaction

 Command :: file -b --mime 
'/tmp/8528c4a1ed4aba84a53e4db3c799d5e320140711-4464-5di5ju.jpg'

 (0.1ms)  rollback transaction
Rendered articles/new.html.erb within layouts/application (2.5ms)
 Completed 200 OK in 258ms (Views: 36.5ms | ActiveRecord: 0.3ms)

My Article model is:

    class Article < ActiveRecord::Base

      belongs_to :category

    attr_accessible :content, :headline, :photo

    has_many :comments

    has_attached_file :photo

    validates_attachment_content_type :photo, :content_type => ["photo/jpg",   
    "photo/jpeg", "photo/png", "photo/gif"]


    end

My articles controller is:

    def create

@category = Category.find(params[:category_id])

# For URL like /categories/1/articles
# Populate an article associate with category 1 with form data
# Category will be associated with the article
@article = @category.articles.build(params[:article])
respond_to do |format|
if @article.save
  format.html { redirect_to category_article_url(@category,@article), notice: 'Article was successfully created.' }
    format.json { render json: @article, status: :created, location: @article }
  # Save the article successfully

else
  format.html { render action: "new" }
    format.json { render json: @article.errors, status: :unprocessable_entity }
  end
end
end

My gemfile is:

    source 'https://rubygems.org'

     gem 'rails', '3.2.13'

    # Bundle edge Rails instead:
    # gem 'rails', :git => 'git://github.com/rails/rails.git'

  gem 'sqlite3'
   gem "paperclip", "~> 4.1"


# Gems used only for assets and not required
# in production environments by default.
group :assets do
 gem 'sass-rails',   '~> 3.2.3'
 gem 'coffee-rails', '~> 3.2.1'

# See https://github.com/sstephenson/execjs#readme for more supported runtimes
# gem 'therubyracer', :platforms => :ruby

  gem 'uglifier', '>= 1.0.3'
end

   gem 'jquery-rails'

I dont know why the transaction is being rollbacked.I am using paperclip for the first time and am really at my wits end.Please help me out


回答1:


You have mistake in you code:

content_type look in log where image upload -> @content_type="image/jpeg" i think this:

validates_attachment_content_type :photo, :content_type => ["photo/jpg",   
    "photo/jpeg", "photo/png", "photo/gif"]

should be:

validates_attachment_content_type :photo, :content_type => /\Aimage/

or

validates_attachment_content_type :photo, :content_type => /^image\/(png|gif|jpeg)/

And paperclip do not save image in db. And read about validation in paperclip, documentation is very useful




回答2:


Docs

Further to Зелёный's answer (which is right), you'll want to check Paperclip's documentation to see the validation aspect:

class User < ActiveRecord::Base
  has_attached_file :avatar, :styles => { :medium => "300x300>", :thumb => "100x100>" }, :default_url => "/images/:style/missing.png"
  validates_attachment_content_type :avatar, :content_type => /\Aimage\/.*\Z/
end

Essentially, the problem you have is to do with file spoofing - the act of checking a file's contents to determine what it has inside (IE if you only allow images, then images alone shall be permitted)

Paperclip never used to have this feature - you basically need to give it the ability to "validate" the correct file type you've uploaded, in your case image/xxxxxx.

The problem you have is you've used photo/jpeg when really it should be image/jpeg, the full answer being Зелёный's



来源:https://stackoverflow.com/questions/24691948/paperclip-image-not-being-added-in-mysql-database

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!