how to upload multiple images with file_column + rmagick in one form? -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
clouder
2008-Mar-29 06:59 UTC
Re: how to upload multiple images with file_column + rmagick
I have done this before using file_column, but have since switched to
attachment_fu. The process should be about the same though. First
off, these links will probably help alot.
http://the-stickman.com/web-development/javascript/upload-multiple-files-with-a-single-file-element/
http://www.silverscripting.com/blog/2007/12/18/multiple-file-upload-with-rails-file_column-plugin-and-mootools/
They both deal with mootools, to make multiple image uploads a bit
snazzier rather than limiting to so many files per form (or littering
your page with a mess of file fields). If you don''t want to use the
snazzy javascripts, then just create your fields and name them
something like ''attachments[]'' so that the files get pushed
into an
array that rails can use like ''params[:attachments]''
As for the controller, mine looks like this (again I''m using
attachment_fu, but it should be identical)
def create
@picture = @item.pictures.build(params[:picture])
@pictures = @item.pictures.find(:all)
unsaved_pictures = []
saved_pictures = []
params[:attachments] ||= []
params[:attachments].each do |file|
@picture = @item.pictures.build({:uploaded_data => file}) unless
file == ""
unsaved_pictures << @picture.filename unless @picture.save
saved_pictures << @picture.filename if @picture.save
end
if saved_pictures.blank?
flash[:error] = "No Pictures were added."
else
if saved_pictures.size < 2
flash[:notice] = "Picture was successfully added.
(#{saved_pictures[0]})"
else
flash[:notice] = "Pictures were successfully added.
(#{saved_pictures.join('', '')})"
end
end
unless unsaved_pictures.blank?
if unsaved_pictures.size < 2
flash[:error] = "File was not added. (#{unsaved_pictures[0]})"
else
flash[:error] = "Files were not added.
(#{unsaved_pictures.join('', '')})"
end
end
respond_to do |format|
format.html { redirect_to(item_pictures_url) }
end
end
The first to lines should be familiar, then I create empty arrays,
saved_pictures and unsaved_pictures so that I can report stuff back to
the user later. Then I iterate through the attachments making sure
the file field wasn''t empty ( I probably should have used file.empty?
instead of file == "" there). If the picture wasn''t saved I
store the
name into my unsaved_pictures array, if it was I store the filename in
saved_puctures array. Then I just use flash to send out notices and
errors depending on what was/wasn''t saved and proper pluralization.
Hope this helps get you going in the right direction.
On a side note if anyone has any suggestions on where I can make this
code better, please do tell me :D The method is a bit beefy and there
are singular/plural flash notices doesn''t seem to dry.
Also has anyone used Paperclip for file attachments yet? If so, how is
it?
On Mar 28, 11:59 pm, Shih Tony
<rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org>
wrote:> how to upload multiple images with file_column + rmagick in one form?
> --
> Posted viahttp://www.ruby-forum.com/.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---