`
xf986321
  • 浏览: 163945 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

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,”

分享到:
评论

相关推荐

    Android 调用系统照相机,相册,上传图片

    在Android开发中,调用系统照相机、相册以及上传图片是常见的功能需求,尤其在社交应用、生活服务类应用中十分常见。本教程将详细讲解如何实现这些功能。 一、调用系统照相机 1. 添加权限:首先在AndroidManifest....

    flutter_open_camera_photo.zip

    例如,`ImagePicker.getImage(source: ImageSource.camera)`会打开相机让用户拍摄照片。 2. **选择相册** 同样利用`image_picker`库,可以选择从用户的设备相册中选取图片。只需将`source`参数设置为`ImageSource....

    android文件上传及写入数据库

    在Android平台上,文件上传和将数据写入数据库是常见的任务,尤其在开发涉及用户交互的应用时,例如照片分享、文档管理或云同步等场景。本文将深入探讨如何在Android环境中实现这两个功能。 首先,让我们讨论文件...

    安卓拍照上传、选择图片上传

    在安卓平台上,用户经常需要进行拍照或从图库选择图片并上传到服务器。这个功能在社交应用、在线购物、文档管理等许多类型的App中都十分常见。本篇将详细讲解如何实现这一功能。 首先,我们需要了解安卓系统提供的...

    获取相机与图库 图片绝对路径

    用户可能需要从相机拍摄新照片或从图库选择已有图片进行编辑、上传等操作。本篇将详细介绍如何实现这一功能。 首先,我们需要在AndroidManifest.xml文件中添加必要的权限,以便访问相机和外部存储: ```xml ...

    多图片上传Upload网页源码

    在Web开发中,用户经常需要上传图片,如个人头像、产品照片或文章配图等。传统的单文件上传方式已经不能满足现代网站的需求,因此,多图片上传功能应运而生。这个"多图片上传Upload网页源码"提供了一个实用的解决...

    PHP基础教程 是一个比较有价值的PHP新手教程!

    不过明确的是编写那样的代码有多简单,购买它们会有多昂贵以及它们需要多么昂贵和强大的硬件。如果你有什么中立的观点(比如说没有被SUN和Microsoft的百万美金所影响),请顺便通知我。 据我所知,JSP基于Java,...

    vue vantUI实现文件(图片、文档、视频、音频)上传(多文件)

    在本例中,上传组件的显示是根据父组件传递过来的`columnName`变量动态控制的,这意味着我们可以在一个页面上根据不同的需求展示不同类型文件的上传控件。通过`v-show`或`v-if`指令,我们可以根据条件来决定是否显示...

    调用系统相机和系统相册,更改图片的保存路径,点击看大图

    在Android应用开发中,调用系统相机和系统相册是常见的功能需求,用户可以通过这些功能拍摄新照片或选择已有的图片。本教程将详细介绍如何实现这两个功能,并讲解如何更改图片的保存路径,以及如何实现点击图片查看...

    Android打开图库选择照片功能代码

    在Android应用开发中,用户经常需要从图库中选择照片以进行各种操作,例如上传、编辑或显示。本文将详细讲解如何实现这个功能,并提供相关的代码示例。 首先,为了访问设备的外部存储(如SD卡)以获取图库中的照片...

    Winform在DataGridView中显示图片

    using (System.IO.FileStream fs = new System.IO.FileStream(path, System.IO.FileMode.Open)) { return System.Drawing.Image.FromStream(fs); } } ``` 在这个方法中,我们使用了`using`语句确保文件流在使用...

Global site tag (gtag.js) - Google Analytics