file_column简单的照片上传

阅读更多
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,”

你可能感兴趣的:(Rails,Git,ActiveRecord,Ruby,Go)