Hi, I am using the excellent file_column extension to Rails. I have a situation where I have multiple files in a form that need to be submitted at once, so I am trying to use the helper as follows: <%= file_column_field "photo[]", "thumbnail_image" %> The idea being that the [] will tell Rails to put the ID in there. The code for the helper is as follows: def file_column_field(object, method, options={}) result = ActionView::Helpers::InstanceTag.new(object, method.to_s+"_temp",self).to_input_field_tag("hidden", {}) result << ActionView::Helpers::InstanceTag.new(object, method, self).to_input_field_tag("file", options) end The code generates the following HTML: <input name="photo[29][thumbnail_image_temp]" type="hidden" id="photo_29_thumbnail_image_temp" /><input name="photo[thumbnail_image]" size="30" type="file" id="photo_thumbnail_image" /> The problem is that the file field itself is missing the ID of the record, so it fails on the update. Can someone who understands the mechanics of the form helpers give me a hint how the file_field helper code above could be modified to properly support the IDs? Thanks, Hunter
I''ve done some more digging but I am not familiar with ActionView internals. To expand on this, the helper creates two form tags, a file tag and a hidden tag. It appears that whichever tag is created first gets the index and the second one does not - I''ve moved them around and the first one always gets the correct index, the second no index at all. Anyone that is familiar with ActionView internals - what is going on here? Thx.> From: Hunter Hillegas <lists-HAWAbpnI61OZ1JSuHaJ1sQC/G2K4zDHf@public.gmane.org> > Reply-To: <rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org> > Date: Tue, 11 Oct 2005 09:29:16 -0700 > To: Rails <rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org> > Conversation: Generating File Fields With Proper Tags > Subject: [Rails] Generating File Fields With Proper Tags > > Hi, > > I am using the excellent file_column extension to Rails. > > I have a situation where I have multiple files in a form that need to be > submitted at once, so I am trying to use the helper as follows: > > <%= file_column_field "photo[]", "thumbnail_image" %> > > The idea being that the [] will tell Rails to put the ID in there. The code > for the helper is as follows: > > def file_column_field(object, method, options={}) > result = ActionView::Helpers::InstanceTag.new(object, > method.to_s+"_temp",self).to_input_field_tag("hidden", {}) > > result << ActionView::Helpers::InstanceTag.new(object, method, > self).to_input_field_tag("file", options) > end > > The code generates the following HTML: > > <input name="photo[29][thumbnail_image_temp]" type="hidden" > id="photo_29_thumbnail_image_temp" /><input name="photo[thumbnail_image]" > size="30" type="file" id="photo_thumbnail_image" /> > > The problem is that the file field itself is missing the ID of the record, > so it fails on the update. > > Can someone who understands the mechanics of the form helpers give me a hint > how the file_field helper code above could be modified to properly support > the IDs? > > Thanks, > Hunter > > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails
Hi, On 10/11/05, Hunter Hillegas <lists-HAWAbpnI61OZ1JSuHaJ1sQC/G2K4zDHf@public.gmane.org> wrote:> <%= file_column_field "photo[]", "thumbnail_image" %> > > The idea being that the [] will tell Rails to put the ID in there. The code > for the helper is as follows: > > def file_column_field(object, method, options={}) > result = ActionView::Helpers::InstanceTag.new(object, > method.to_s+"_temp",self).to_input_field_tag("hidden", {}) > > result << ActionView::Helpers::InstanceTag.new(object, method, > self).to_input_field_tag("file", options) > end >the reason why only the first tag has the object''s id included in its name is rather subtle: InstanceTag.new performs a gsub! on the object parameter so that the "[]" in the object''s name are removed for the second call. I think InstanceTag should be changed so as not to modify its parameters, but for the time being you can work around this by modifying the helper to look like this: def file_column_field(object, method, options={}) result = ActionView::Helpers::InstanceTag.new(object.dup, method.to_s+"_temp",self).to_input_field_tag("hidden", {}) result << ActionView::Helpers::InstanceTag.new(object.dup, method, self).to_input_field_tag("file", options) end This will be included in the next version of file_column as well. Sebastian
Perfect. Thanks Sebastian! Thanks again for a great tool.> From: Sebastian Kanthak <skanthak-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > Reply-To: <sebastian.kanthak-ZS8b95Whz3sUSW6y5lq3GQ@public.gmane.org>, <rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org> > Date: Tue, 11 Oct 2005 19:58:07 +0200 > To: <rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org> > Subject: Re: [Rails] Generating File Fields With Proper Tags > > Hi, > > On 10/11/05, Hunter Hillegas <lists-HAWAbpnI61OZ1JSuHaJ1sQC/G2K4zDHf@public.gmane.org> wrote: >> <%= file_column_field "photo[]", "thumbnail_image" %> >> >> The idea being that the [] will tell Rails to put the ID in there. The code >> for the helper is as follows: >> >> def file_column_field(object, method, options={}) >> result = ActionView::Helpers::InstanceTag.new(object, >> method.to_s+"_temp",self).to_input_field_tag("hidden", {}) >> >> result << ActionView::Helpers::InstanceTag.new(object, method, >> self).to_input_field_tag("file", options) >> end >> > > the reason why only the first tag has the object''s id included in its > name is rather subtle: InstanceTag.new performs a gsub! on the object > parameter so that the "[]" in the object''s name are removed for the > second call. I think InstanceTag should be changed so as not to modify > its parameters, but for the time being you can work around this by > modifying the helper to look like this: > > def file_column_field(object, method, options={}) > result = ActionView::Helpers::InstanceTag.new(object.dup, > method.to_s+"_temp",self).to_input_field_tag("hidden", {}) > > result << ActionView::Helpers::InstanceTag.new(object.dup, method, > self).to_input_field_tag("file", options) > end > > This will be included in the next version of file_column as well. > > Sebastian > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails