转移 http://aikin.me/2014/02/20/rails-paperclip/
Paperclip是 Rails 的一个上传图片插件,它和ImageMagick联合使用,可以很方便的实现图片上传并切割指定大小的功能,使整个图片上传过程非常简单,还会将上传的图片保存在public文件夹下。
1.下载paperclip gem包
(1)在Gemfile中写上
gem 'paperclip'
(2)在终端运行
bundle install
2.使用paperclip
(1)在终端输入:
rails g paperclip image_news image
这样就会在原先的 image_news model中增加image属性,同时在app/db/migrate 文件夹下会
新建xxxxxxx_add_attachment_image_to_image_news.rb 文件内容:
class AddAttachmentImageToImageNews < ActiveRecord::Migration
def self.up
change_table :image_news do |t|
t.attachment :image
end
end
def self.down
drop_attached_file :image_news, :image
end
end
(2)再对数据库迁移,终端输入:
rake db:migrate
如果你原先的image_news model 中没有其他属性,那么在schema.rb文件中:
ActiveRecord::Schema.define(version:xxxxxxx) do
create_table "image_news", force: true do |t|
t.datetime "created_at"
t.datetime "updated_at"
t.string "image_file_name"
t.string "image_content_type"
t.integer "image_file_size"
t.datetime "image_updated_at"
end
end
(3)接着在image_news.rb文件中写上:
class ImageNews < ActiveRecord::Base
has_attached_file :image, //:styles => { :small => "150x150>"},
:url => '/images/:id/:style/:basename.:extension',
:path => ':rails_root/public/images/:id/:style/:basename.:extension'
validates_attachment_presence :image
validates_attachment_size :image, :less_than => 5.megabytes
validates_attachment_content_type :image, :content_type => %w(image/jpeg image/png)
end
上面代码,要使用 :styles 就必须要确认是否有装 imageMagick,如果没装就会报错。
还有 :path :rails_root/public 后面的必须和 :url相同,就像这里的
'images/:style/:basename.:extension'
:rails_root The path to the Rails application.(当前Rails程序的路径,等同于RAILS_PATH的值。) :rails_env The current environment (e.g. development, production)(当前运行环境,如:development,production) :class The class name of the model that the attachment is part of, underscored and pluralised for your convenience.(文件拥有者的类名的复数形式,多个单词间以下划线连接。) :basename The name of the originally uploaded file without its extension.(上传的文件原始的名字,不带扩展名。) :extension The file extension of the originally uploaded file.(上传的文件的扩展名) :id The ID of the model that the attachment is part of.(文件拥有者的id) :id_partition The same as :id but formatted as a string using ID partitioning.(和:id一样,但结果是用ID partitioning格式化过的。) :attachment The name of the attachment attribute (defined in the call to has_attached_file) downcased and pluralised for your enjoyment.(文件作为拥有者的属性的属性名,也就是跟在has_attached_file后面的那个名字,小写、复数形式。) :style The current style of the attachment file being processed (e.g. in the ‘discarding an uploaded image‘ example above the :style would be one of ‘original’ or ‘small’) (文件被以某种样式处理的样式名,该选项用于图片处理。)
(4) 在controller(controllers/image_upload_controller.rb)里写上:
class ImageUploadController < ApplicationController
def image_upload_view
@image_news = ImageNews.new
end
def upload
@image_news = ImageNews.new
@image_news.image = params[:image_news][:image]
@image_news.save
redirect_to(:action => 'image_show_view', :id => @image_news.id)
end
def image_show_view
@image_news = ImageNews.find(params[:id])
end
end
(5) 在view(views/image_upload_view.html.erb)写上:
<%= form_for @image_news, :url =>{:action => 'upload'}, :html => {:multipart => true} do |f| %>
请选择图片:
<%= f.file_field :image%>
<%= f.submit '上传' %>
<% end %>
(6)在view/image_show_view.html.erb写上:
<%= image_tag @image_news.image.url%>
(7) 安装ImageMagick (Mac OS X 10.8.5) 安装很成功、方便,不过得最先装xcode。
curl -O ftp://ftp.imagemagick.org/pub/ImageMagick/ImageMagick.tar.gz tar -zxf ImageMagick.tar.gz cd ImageMagick-*/ ./configure --prefix=/opt/local make sudo make install
也可以用Homebrew安装。
参考:http://yuan.iteye.com/blog/604174
http://huacnlee.com/blog/rails-plugin-paperclip-for-image-upload/
http://railscasts.com/episodes/134-paperclip
http://elf8848.iteye.com/blog/455675