在网上找了关于paperclip的有关插件,发现按他们说的,设置了大小却没有效果,后来发现需要安装一个图像编辑的软件才行:ImageMagick
[b]下面简单的介绍一下:[/b]
环境:Windows + Cygwin + Rails
向项目中的Person类添加上传附件功能
[b]一、安装paperclip:[/b]
[b]迁移任务:[/b]
[b]在model/Person.rb中加入:[/b]
[b]在views/people下的new.html.erb和edit.html.erb中修改form并加入:[/b]
在views/people下的index.html.erb和show.html.erb中加入:
不过现在上传了图片还不能正确的显示出来,还需要:
[b]二、安装ImageMagick[/b]
详见:[url]http://www.imagemagick.org/script/install-source.php#unix[/url]
大致是先下载ImageMagick.tar.gz到Cygwin目录下,解压编译安装:
现在就可以啦,试试吧
[img]http://danny-chiu.iteye.com/upload/picture/pic/36291/66536de3-8cc8-31b8-b1f2-12afdb2ab659.jpeg[/img]
[b]下面简单的介绍一下:[/b]
环境:Windows + Cygwin + Rails
向项目中的Person类添加上传附件功能
[b]一、安装paperclip:[/b]
script/plugin install git://github.com/thoughtbot/paperclip.git
[b]迁移任务:[/b]
script/generate paperclip person photo
rake db:migrate
[b]在model/Person.rb中加入:[/b]
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
[b]在views/people下的new.html.erb和edit.html.erb中修改form并加入:[/b]
<% 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) %>
不过现在上传了图片还不能正确的显示出来,还需要:
[b]二、安装ImageMagick[/b]
详见:[url]http://www.imagemagick.org/script/install-source.php#unix[/url]
大致是先下载ImageMagick.tar.gz到Cygwin目录下,解压编译安装:
tar xvfz ImageMagick.tar.gz
cd ImageMagick-6.5.1
./configure
make && make install
现在就可以啦,试试吧
[img]http://danny-chiu.iteye.com/upload/picture/pic/36291/66536de3-8cc8-31b8-b1f2-12afdb2ab659.jpeg[/img]