我正在尝试使用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=#<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)
我的文章模型是:
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
我的文章控制器是:
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
我的宝石文件是:
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'
我不知道为什么交易正在回滚。我第一次使用回形针,我真的在我的智慧结束。请帮帮我
答案 0 :(得分:1)
你的代码有误:
content_type
查看图片上传的日志 - &gt; @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 in paperclip,文档非常有用
答案 1 :(得分:0)
<强>文档强>
继Зелёный
的回答(正确)之后,您还要检查Paperclip's documentation以查看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
基本上,你遇到的问题是与file spoofing有关 - 检查文件内容以确定其内容的行为(IE如果你只允许图像,那么只允许图像) )
Paperclip从未使用此功能 - 您基本上需要赋予它“验证”功能。您已经上传的正确文件类型image/xxxxxx
。
你遇到的问题是你使用photo/jpeg
时应该是image/jpeg
,完整的答案是Зелёный
&#39>