活用插件attachment_fu,在rails中以AJAX方式上传文件
当前位置:首页 ----> Web开发 ----> Ruby/Python
关键词:attachment fu,rails,AJAX,attachment,to parent,file upload plugin,list,rails Step,demo,rails demo,asset,migration,Javascript prototype scriptaculous,layout,index rhtml,form rhtml,item rhtml,rb,create,config
weskycn:
这篇文章主要解决的问题是用attachment插件和responds_to_parent插件实现ajax方式上传文件(图片)的目的。 Step 1. Choose a file upload plugin(选择上传插件) 可用的上传插件有如下三种: [list=]1.file_column  2.acts_as_attachment  3.attachment_fu [/list] 推荐attachment fu+rails1.2. Step 2. 选择安装Rmagic  gem install rmagic Step 3. 新建项目并下载插件attachment_fu 新建项目demo 引用rails demo 安装插件attachment_fu: 引用cd demo ruby script/plugin install http://svn.techno-weenie.net/projects/plugins/attachment_fu/ Step 4. 开始编写代码 用下面命令创建一个rails资源asset 引用ruby script/generate scaffold_resource asset filename:string content_type:string size:integer width:integer height:integer parent_id:integer thumbnail:string created_at:datetime 下面是自动生成的migration,可自行修改,在此我们使用默认。
class CreateAssets < ActiveRecord::Migration
 def self.up
 create_table :assets do |t|
 t.column :filename, :string
 t.column :content_type, :string
 t.column :size, :integer
 t.column :width, :integer
 t.column :height, :integer
 t.column :parent_id, :integer
 t.column :thumbnail, :string
 t.column :created_at, :datetime
 end
 end

 def self.down
 drop_table :assets
 end
end
在模型中,我们加入以下代码:  :Rmagick 
end Step 5. AJAX it 把Javascript prototype/scriptaculous 加入layout. 安装插件responds_to_parent ruby script/plugin install http://sean.treadway.info/svn/plugins/responds_to_parent/ 修改index.rhtml: 新建一个局部模版文件_form.rhtml: 新建一个局部模版文件_list_item.rhtml: 修改assets_controller.rb中的create方法如下:  @asset.errors.to_xml }
 format.js do
 responds_to_parent do
 render :update do |page|
 # update the page with an error message
 end
 end 
 end
 end
 end
end 最后别忘了,修改config文件夹中的database.yml文件,创建相应的数据库,并执行命令: rake db:migrate
ruby script/server 然后到浏览器里面查看效果吧! 注:本文翻译自http://khamsouk.souvanlasy.com/。更详细的介绍请访问原文。另外本篇所介绍的内容经过测试没有问题。欢迎讨论交流            <img alt="6a3d2798-a492-426a-9fe4-d7bd3fffac0b-thumb" class="magplus" src=http://www.javaeye.com/upload/attachment/9057/6a3d2798-a492-426a-9fe4-d7bd3fffac0b-thumb.jpg?1199174551 title="点击查看原始大小图片" />   描述: 效果图  大小: 2.3 MB  查看次数: 184            demo1.rar (113 KB)  描述: 我做的例子  下载次数: 97


luodongju325:
第4步的时候我是用上面的代码创建asset,但总是出现 "could't find 'scaffold_resource' generator


luodongju325:
在controller的creat中可以这样写吧,我怎样会报错呀  @asset


weskycn:
请教你的rails版本是多少?最起码要rails_1.2.3的。<img src=http://www.javaeye.com/images/smiles/icon_redface.gif/>


weskycn:
<img src=http://www.javaeye.com/images/smiles/icon_cry.gif/>


zyyseu:
undefined method `has_attachment' for Asset:Class 这个错误怎样办?


weskycn:
插件没有装好吧?您把具体错误贴出来,还有,rmagic装了吗?您看看附件里我做的例子吧(新增)。We.Sky


zyyseu:
我用的是netbean,刚学着用的,还不熟 但是在project的vender目录下有几个需要安装的插件的,rmagick,和attachment_fu都有


zyyseu:
跑了您的程序,缩略图显示不出来,楼主,大概是什麽原因呢?


weskycn:
不好意思,这几天一直没有上javaeye! rmagick是gem到ruby里的,我以为您应当检查下您的rmagic安装是否正确,参考资料:http://www.javaeye.com/topic/69518 另外,假如还不行,继续在下面发帖子。尽量帮您弄好。


zyyseu:
楼主客气了. 按着您所给的介绍安装rmagick的方法进行安装,还是不行,不知道问题出在哪... 我用的是netbean的开发环境,内嵌的jruby


weskycn:
您是把rmagic安装到插件里了么?rmagic不应当在vender下面。


zxwilde:
http://sean.treadway.info/svn/plugins/responds_to_parent/ 该地址无法下载安装responds_to_parent插件,不知道,是不是我自身的问题。Plugin not found: ["http://sean.treadway.info/svn/plugins/responds_to_parent/"]


weskycn:
给我您的邮箱,我直接发给您。


weskycn:
呵呵,附件demo1.rar里就有responds_to_parent这个插件,直接拷贝到/vender/plugin/下就可以了


zxwilde:
zhangxin@kingskysoft.com zxwilde@sina.com 麻烦您了,感谢!


weskycn:
已发送到第一个邮箱里了,查收


Auckland:
我也参考  http://clarkware.com/cgi/blosxom/2007/02/24 自己写了个小demo。所需要的 东西都安装了,程序运行也没有题。 但是没有缩略图,只有原大小的图片。 rails 环境是 2.0.1,rmagic 2.1.0  :Rmagick }
好像是rmagick没有生成thumbnails.只是把原图上传到文件系统了。 如何解决呢?
原文出处:http://www.javaeye.com/topic/123777