Rails Paperclip and ImageMagick

 在网上找了关于paperclip的有关插件,发现按他们说的,设置了大小却没有效果,后来发现需要安装一个图像编辑的软件才行:ImageMagick

[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]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值