我正在尝试使用回形针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'我不知道为什么交易会被卷起来。我第一次用回形针,我真的很聪明,end.Please帮我解决这个问题
发布于 2014-07-11 07:20:46
你的代码有错误:
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中。和阅读回形针验证,文档是非常有用的
发布于 2014-07-11 09:17:03
Docs
对于Зелёный的回答(这是正确的),您需要检查回形针文献以查看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,如果您只允许图像,那么仅允许图像)。
回形针从来没有这样的功能--你基本上需要赋予它“验证”你上传的正确文件类型的能力,在你的例子中是image/xxxxxx。
你的问题是你使用了photo/jpeg,而实际上应该是image/jpeg,完整的答案是Зелёный的。
https://stackoverflow.com/questions/24691948
复制相似问题