file_column是一款相当不错的,用于上传文件,生成缩略图的一款ROR插件
下面介绍使用方法
安装为组件
./script/plugin install
http://opensvn.csie.org/rails_file_column/
plugins/file_column/trunk
在model里修改
validates_format_of :image,
:with=>/^.*(.jpg|.JPG|.gif|.GIF)$/,
:message => "你只能上传JPG或则GIF的图片文件"
file_column :image, :magick => {
:versions => { "thumb" => "50x50", "medium" => "640x480>" }
}
注意上传文件会出错,有时是因为少了这句<%= start_form_tag 'create',:multipart => true%>
Just make the "image" column ready for handling uploaded files...
class Entry < ActiveRecord::Base
file_column :image
end
在view里修改:
... generate file fields that keep uploaded images during form redisplays to your view...
<%= file_column_field "entry", "image" %>
... and display uploaded images in your view:
<%= image_tag url_for_file_column("entry", "image") %>
注意在数据库中,因为要生成缩小图,所以必须在 pic blob not null后加入 pic_temp blob not null一句。