在网上找了关于paperclip的有关插件,发现按他们说的,设置了大小却没有效果,后来发现需要安装一个图像编辑的软件才行:ImageMagick
下面简单的介绍一下:
环境:Windows + Cygwin + Rails
向项目中的Person类添加上传附件功能
一、安装paperclip:
script/plugin install git://github.com/thoughtbot/paperclip.git
迁移任务:
script/generate paperclip person photo
rake db:migrate
在model/Person.rb中加入:
class Person < ActiveRecord::Base
has_attached_file :photo, :styles => {
:thumb => "100x100#",
:small => "150x150>",
:large => "400x400>" },
:url => "/assets/products/:id/:style/:basename.:extension",
:path => ":rails_root/public/assets/products/:id/:style/:basename.:extension"
end
在views/people下的new.html.erb和edit.html.erb中修改form并加入:
<% form_for @person, :html => { :multipart => true } do |f| %>
<%= f.error_messages %>
<p>
<%= f.label :photo %><br />
<%= f.file_field :photo %>
</p>
<% end %>
在views/people下的index.html.erb和show.html.erb中加入:
<%= image_tag person.photo.url(:thumb) %>
<%= image_tag person.photo.url(:small) %>
<%= image_tag person.photo.url(:large) %>
不过现在上传了图片还不能正确的显示出来,还需要:
二、安装ImageMagick
详见:
http://www.imagemagick.org/script/install-source.php#unix
大致是先下载ImageMagick.tar.gz到Cygwin目录下,解压编译安装:
tar xvfz ImageMagick.tar.gz
cd ImageMagick-6.5.1
./configure
make && make install
现在就可以啦,试试吧