Folders and files
| Name | Name | Last commit date | ||
|---|---|---|---|---|
Repository files navigation
Xiaoming Lu's changes:
Background
========
Need a way to upload image using file_column, save image to tmp, upload the
image to Amazon s3 in before_save, and then save the Amazon cloud url (starting with
http://) to database.
FEATURES
========
Let's assume an model class named Entry, where we want to define the "image" column
as a "file_upload" column.
class Entry < ActiveRecord::Base
file_column :image
before_save :update_image_url
def update_image_url
if ((self.image_url != nil) and File.exists?(self.image_url))
ss = MetaChannels::Thumbnails::StorageStrategy::AmazonS3StorageStrategy.new
new_image_url = ss.store_featured_item(self) #store tmp image to s3, update image_url with s3 url
new_image_url = new_image_url.to_s().strip()
self.image_url = new_image_url
else
# after user upload a file, the file path needs to be valid so that file can be uploaded to s3
logger.error("ERROR: image file does not exists")
end
end
end
* every entry can have one uploaded file, the filename will be stored in the "image" column
* files will be stored in "tmp", then file will be opened from tmp and upload to Amazon s3,
tmp file will be deleted
* When Entry is saved, Amazon Cloud url of the file (starting with "http://" will be saved
into the database.
* in a view, "<%= file_column_field 'entry', 'image' %> will create a file upload field as well
as a hidden field to recover files uploaded before in a case of a form redisplay
* in a view, add Entry/_image_url_form_column.html.erb to display image.
<label><%=column.name.to_s.titleize %>:</label>
<dl>
<dt> <label><%=column.label%>:</label> </dt>
<dd>
<% if (@record.image_url and @record.image_url.include?("http")) %>
<%=link_to(image_tag(@record.image_url, {:width=>"50", :height=>"50"}), @record.image_url, :popup => true)%>
<% elsif @record.image_url %>
"NO IMAGE (<%=@record.image_url%>)"
<% else %>
"NO IMAGE"
<% end %>
<br/> Image Upload: <%= file_column_field 'record', 'image_url' %>
</dd>
</dl>
Note that url_for_file_column is deprecated in this file_column and please do not use it.
USAGE
=====
Just drop the whole directory into your application's "vendor/plugins" directory. Starting
with version 1.0rc of rails, it will be automatically picked for you by rails plugin
mechanism.
BUGS & FEEDBACK
===============
Bug reports (as well as patches) and feedback are very welcome. Please send it to
xiaoming.lu.backup@gmail.com