taken from here using acts_as_fu, possible work for acts as attachment as well http://beast.caboo.se/forums/2/topics/870 Image.transaction do images = params[:image].collect { |img| Image.create(:uploaded_data => img } end unless params[:image].nil? basically i think i can have a form with a dynamic amount of file fields for each file i would like to upload. Image i guess is the table that will be storing the photos. i am confused at the block part. basically is this converting the hash into an array, going through each index in the array, and saving each photo into the Image table? -- 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 -~----------~----~----~----~------~----~------~--~---
treebeard wrote:> taken from here using acts_as_fu, possible work for acts as attachment > as well > http://beast.caboo.se/forums/2/topics/870 > > Image.transaction do > images = params[:image].collect { |img| Image.create(:uploaded_data => > img } > end unless params[:image].nil? > > > basically i think i can have a form with a dynamic amount of file fields > for each file i would like to upload. Image i guess is the table that > will be storing the photos. > > i am confused at the block part. basically is this converting the hash > into an array, going through each index in the array, and saving each > photo into the Image table?Collect takes every element in the array and replaces it with the result of the passed block. The unless after the transaction block prevent the block from executing if the condition is true. So you could rewrite the code like this: unless params[:image].nil? Image.transaction do images = [] params[:image].each do |img| images << Image.create(:uploaded_data => img) end end end But what fun is that? Although, personally I don''t like a condition after a multi-line block like that. You don''t realize it will be conditionally executed until the end of it. But it''s a great case for the use of collect. -- 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 -~----------~----~----~----~------~----~------~--~---
> images=params[:image].collect{|img|Image.create(:uploaded_data=>img} > > i am confused at the block part. basically is this converting the hash > into an array, going through each index in the array, and saving each > photo into the Image table? >Assuming Image is a plain old ActiveRecord class (no overwritten methods) then yes. The collect would iterate over every image saving the records (the records would contain just two fields with values: id and uploaded_data) and returning finally an array with the AR objects created. regards javier ramirez -- -------- Estamos de estreno... si necesitas llevar el control de tus gastos visita http://www.gastosgem.com !!Es gratis!! --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
hi Alex, thanks for the post. however, im still trying to understand this... images = [] params[:image].each do |img| images << Image.create(:uploaded_data => img) end params[:image]....the actual file is here correct? params[:image][:uploaded_data] and is that the same as this? params[''0''][:uploaded_data] params[''1''][:uploaded_data] etc... for multiple files? also, what is the purpose for images? cant one just call Image.create(:uploaded_data => img) inside the block? thanks. Alex Wayne wrote:> treebeard wrote: >> taken from here using acts_as_fu, possible work for acts as attachment >> as well >> http://beast.caboo.se/forums/2/topics/870 >> >> Image.transaction do >> images = params[:image].collect { |img| Image.create(:uploaded_data => >> img } >> end unless params[:image].nil? >> >> >> basically i think i can have a form with a dynamic amount of file fields >> for each file i would like to upload. Image i guess is the table that >> will be storing the photos. >> >> i am confused at the block part. basically is this converting the hash >> into an array, going through each index in the array, and saving each >> photo into the Image table? > > Collect takes every element in the array and replaces it with the result > of the passed block. The unless after the transaction block prevent the > block from executing if the condition is true. > > So you could rewrite the code like this: > > unless params[:image].nil? > Image.transaction do > images = [] > params[:image].each do |img| > images << Image.create(:uploaded_data => img) > end > end > end > > But what fun is that? Although, personally I don''t like a condition > after a multi-line block like that. You don''t realize it will be > conditionally executed until the end of it. But it''s a great case for > the use of collect.-- 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 -~----------~----~----~----~------~----~------~--~---
> params[:image]....the actual file is here correct?If you look at the form, it''s probably using a group of file fields with a name of ''image[]''. Get params have no concept of arrays, so this is basically posted like this: image[]=FILE1&image[]=FILE2 Rails parses fields with names ending in [] into an array. So params[:image] is an array of the images from the file field.> params[:image][:uploaded_data] > and is that the same as this?Nope.> params[''0''][:uploaded_data] > params[''1''][:uploaded_data] > > etc... for multiple files?It''s an array... params[:image][0]> also, what is the purpose for images? cant one just call > > Image.create(:uploaded_data => img) inside the block?Yup. Alex was just showing an example using #each instead of #collect. #collect will return an array of the created images. With #each, you have to populate an images array manually. -- Rick Olson http://lighthouseapp.com http://weblog.techno-weenie.net http://mephistoblog.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 -~----------~----~----~----~------~----~------~--~---
On Mar 14, 2007, at 7:52 PM, Alex Wayne wrote:> Collect takes every element in the array and replaces it with the > result > of the passed block.Just for the record, collect does not modify the array in place, in that sense no element is replaced. (Replacement is provided by collect!) -- fxn --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
thanks all and thanks rick for the code you posted over in beast! -- 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 -~----------~----~----~----~------~----~------~--~---