file_column and how the fuck it handles multiple files?
Handling multiple files with file_column plugin can be quite nifty and the most of the time its lack of documentation that makes thing challenging.
So here goes my code, without much talk:
<form action='updater' method='post' enctype="multipart/form-data">
<table>
<tr>
<td> Tag :</td><br>
<td><input type="text" name="picture[tag]" /></td>
</tr>
<% for i in 1..5 %>
<tr>
<td> File :</td>
<td>
<input id="<%= "picture_#{i}_filename_temp" %>" name="<%= "picture[#{i}][filename_temp]" %>" type="hidden" />
<input id="<%= "picture_#{i}_filename" %>" name="<%= "picture[#{i}][filename]" %>" size="30" type="file" />
</td>
</tr>
<% end %>
<tr>
<td> </td>
<td><%= submit_tag "Upload" %></td>
</tr>
</table>
</form>
and here goes my controller code:
def updater
user_id = session[:user].id
Picture.current_user_id(user_id)
tag = params[:picture][:tag]
logger.info("And tag is #{tag}")
params[:picture].each do |file_id,attr|
logger.info("Somehow we reached inside the loop and file is is #{file_id}")
begin
file_id = file_id.to_i
if file_id>=1 and file_id <=5
logger.info("File ID is #{file_id}")
picture = Picture.new(attr)
picture[:tag] = tag unless tag.nil?
picture[:user_id] = user_id
if picture.save
logger.info("Picture saved")
end
end
rescue
logger.info("#{$!}")
end
end
redirect_to :action => 'list'
end
And here goes my model code:
class Picture < ActiveRecord::Base
file_column :filename, :magick => {
:versions =>
{
:small => {:crop => "1:1", :size => "155×155!" },
:thumb => {:crop => "1:1", :size => "50×50!" },
:medium => { :size => "640×480" }
}
}
validates_presence_of(:filename, :message => "Please enter a filename")
def self.current_user_id(temp_user_id)
@current_user = temp_user_id.to_s
end
end
Note that, i am not using file_column_field tag helper…because somehow it doesn't like file arrays…so i rather stuck to my own devices.I am not in mood of much typing now..so if you have doubt…feel free to mail me.
Hemant "Tailu" said,
May 29, 2006 at 7:31 am
Nope Nope , What is this ?? Have u implemented it somewhere , Or written in ur blog to just store it in ur account . At least tell what was ur reqmt behind this Ghosty Code ??
matt said,
July 24, 2006 at 6:27 pm
Good job Hemant. Keep up the good work!