Paperclip图像未添加到mysql数据库中

我试图使用paperclip gem 4在数据库中插入图像,但我在rails控制台中收到以下错误:

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=#>}, "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) 

我的文章模型是:

  class Article  ["photo/jpg", "photo/jpeg", "photo/png", "photo/gif"] end 

我的文章控制器是:

  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 

我的gemfile是:

  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' 

我不知道为什么交易正在回滚。我第一次使用回形针,我真的在我的智慧结束。请帮帮我

您的代码有误:

content_type查看日志中图片上传 – > @content_type="image/jpeg"我认为:

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

应该:

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

要么

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

并且回形针不会在db中保存图像。 并阅读回形针中的validation ,文档非常有用

文件

Зелёный的答案(这是正确的)之后,您需要查看Paperclip的文档以查看validation方面:

 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 

基本上,你遇到的问题是文件欺骗 – 检查文件内容以确定其内部的行为(IE如果你只允许图像,那么只允许图像)

Paperclip从未使用此function – 您基本上需要让它能够“validation”您上传的正确文件类型,在您的案例image/xxxxxx

你遇到的问题是你使用photo/jpeg时应该是image/jpeg ,完整的答案是Зелёный