---
title: "paperclip学习使用笔记"
author: "jackdong"
date: "2010-03-07T22:43:31Z"
tags: []
categories: ["编程语言"]
slug: "paperclip"
draft: false
lastmod: "2010-03-07T22:43:31Z"
id: 609827
url: "https://dnnta.iteye.com/blog/609827"
---

[**paperclip**](http://wiki.github.com/thoughtbot/paperclip/) 是rails处理附件的一个插件， 相对于以往的[**attachment\_fu**](http://github.com/technoweenie/attachment_fu)等在效率和使用上更胜一筹。

 

  **paperclip**上传的图片附件如果不需要进行改变大小等操作， 则不需要安装ImageMagick。paperclip在window平台下进行图片处理很容易得到这样的错误信息

```
。。。is not recognized by the 'identify' command
```

 花了1.5h 终于在一个被墙的blog上找到这样的一个解决办法： 在项目的config/iniinitializers目录下面新建一个paperclip.rb文件 并写入如下代码:

```
Paperclip.options[:command_path] = 'D://Program Files//ImageMagick'  # 你的imageMagick安装目录
Paperclip.options[:swallow_stderr] = false
```

 

 修改User模型 添加如下代码

```
has_attached_file :avatar, :styles => { :medium => "200x200>", :thumb => "32x32>" }
```

 执行下面代码 将为users表添加4列

```
script/generate paperclip User avatar 
```

 

表单：

```
<% form_for current_users, :html => {:multipart => true} do|form|%>       
　<%= form.file_field :avatar %>
   <%=form.submit '上传' %>
<% end %> 
```

 

因为User模型是利用restful\_authentication插件生成的， 如果就这样上传的话是不行的，上传的附件被过滤掉了。 上传后Users表中有关附件的列仍然为nill， 再看控制台信息：

```
WARNING: Can't mass-assign these protected attributes: avatar
```

 上网搜了一下 大致明白是撒原因了 原来restful\_authentication帮我们生成的User模型中有这样的一些话

```
# anything else you want your user to change should be added here.
  attr_accessible :login, :email, :password, :password_confirmation
```

 在attr\_accessible后面添加上：avater就ok了。

 

 至于paperclip的其他使用方法 后面再继续研究。今天仅记录使用过程遇到的问题。