file_column简单的照片上传

介绍了一个名为file_column的Ruby on Rails库,它极大简化了处理文件上传的过程,并提供了如缩略图生成等实用功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.file_column 
git clone git://github.com/activescaffold/file_column.git 
gem install rmagick
model: 
file_column :photo, :magick => { 
    :versions => { "thumb" => "100x100>", "medium" => "640x480>" }
  }
view:
<%= image_tag(url_for_file_column('profile', 'photo', 'thumb')) if @profile && @profile.photo %><%= file_column_field 'profile', 'photo' %>


web example:
Attention: I will be speaking at the Canada on Rails conference, which will be held on April 13th-14th in Vancouver, Canada. David Heinemeier-Hansson will be giving a keynote and there are a lot of other big names on the list of speakers. I'm honored to give a talk about file_column and developing plugins in general.

This library makes handling of uploaded files in Ruby on Rails as easy as it should be. It helps you to not repeat yourself and write the same file handling code all over the place while providing you with nice features like keeping uploads during form redisplays, nice looking URLs for your uploaded files and easy integration with RMagick to resize uploaded images and create thumb-nails. Files are stored in the filesystem and the filename in the database.
Example

As you can judge a library best by looking at how to use it, here is a short example:

Just make the "image" column ready for handling uploaded files...


    class Entry < ActiveRecord::Base
        file_column :image
    end
    

... generate file fields that keep uploaded images during form redisplays to your view...


    <%= file_column_field "entry", "image" %>
    

... and display uploaded images in your view:


    <%= image_tag url_for_file_column("entry", "image") %>
    

It's just as easy! Why should it be any more difficult for a Rails application?

So what about the RMagick integration? Have a look:

To resize every uploaded image to a maximum size of 640x480, you just have to declare an additional option.


    class Entry < ActiveRecord::Base
        file_column :image, :magick => { :geometry => "640x480>" }
    end
    

You can even automatically create versions in different sizes that have nice filenames...


    class Entry < ActiveRecord::Base
        file_column :image, :magick => { 
          :versions => { "thumb" => "50x50", "medium" => "640x480>" }
        }
    end
    

... and display them in your view:

     <%= image_tag url_for_file_column("entry", "image") %>
     
值得注意的是,如果你用在其他的页面显示这张图片,必须存在这个实例变量@xxx
eg.
<% @profiles.each do |@profile|%>
<%= image_tag(url_for_file_column('profile', 'photo', 'thumb')) if @profile.photo %>
end

bug:
uninitialized constant FileColumn::ClassMethods::Inflector

solution:
go to file_column.rb
go to line 619
add “ActiveSupport::” before “Inflector.underscore(self.name).to_s,” to give =>
“ActiveSupport::Inflector.underscore(self.name).to_s,”

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值