没有在 mysql 数据库中添加回形针图像 [英] Paperclip Image not being added in mysql database
问题描述
我正在尝试使用 paperclip gem 4 在数据库中插入图像,但我的 Rails 控制台出现以下错误:
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)
我的文章模型是:
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
我的 gemfile 是:
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
推荐答案
你的代码有误:
content_type
查看图片上传的日志 -> @content_type="image/jpeg"
我认为:
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"]
应该是:
validates_attachment_content_type :photo, :content_type => /\Aimage/
或
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
这篇关于没有在 mysql 数据库中添加回形针图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!