Hiya In my view, I''m using <%= f.file_field :file %> Problems is, if the form has a validation error, this field is cleared - and the user has to reselect a file Does anyone have a solution for this? thanks Jason --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Does anyone have any ideas? Is it even possible to fix? Have been googling, but no luck --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Mark Reginald James
2008-Jan-31 18:57 UTC
Re: file upload field empty when validation error
jasonb wrote:> In my view, I''m using <%= f.file_field :file %> > > Problems is, if the form has a validation error, this field is cleared > - and the user has to reselect a file > > Does anyone have a solution for this?HTML file select controls are always initially blank in Rails, and this is also enforced by most browsers. This is because a malicious web page could get you to upload an arbitrary file by hiding the select from the user. -- We develop, watch us RoR, in numbers too big to ignore. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Ok but this is after the form his submitted and returned with errors Can anyone advise of a way to get around this? I''m not sure of the nature of the object returned by file_field.... does it contain the file path? am I able to re-populate the file field based on that object? On Feb 1, 5:57 am, Mark Reginald James <m...-bzGI/hKkdgQnC9Muvcwxkw@public.gmane.org> wrote:> jasonb wrote: > > In my view, I''m using <%= f.file_field :file %> > > > Problems is, if the form has a validation error, this field is cleared > > - and the user has to reselect a file > > > Does anyone have a solution for this? > > HTML file select controls are always initially blank in Rails, > and this is also enforced by most browsers. This is because > a malicious web page could get you to upload an arbitrary file > by hiding the select from the user. > > -- > We develop, watch us RoR, in numbers too big to ignore.--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Maybe I could ask this.... When a form is submitted with file_field.... what is the object that is generated from the file_field?..... and what is the best way to inspect that object? For example, if I wanted to output that object with the validation- errors, for the sake of debugging, how would I do it? On Feb 4, 7:44 pm, jasonb <jason.bower...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Ok but this is after the form his submitted and returned with errors > > Can anyone advise of a way to get around this? > > I''m not sure of the nature of the object returned by file_field.... > does it contain the file path? am I able to re-populate the file > field based on that object? > > On Feb 1, 5:57 am, Mark Reginald James <m...-bzGI/hKkdgQnC9Muvcwxkw@public.gmane.org> wrote: > > > jasonb wrote: > > > In my view, I''m using <%= f.file_field :file %> > > > > Problems is, if the form has a validation error, this field is cleared > > > - and the user has to reselect a file > > > > Does anyone have a solution for this? > > > HTML file select controls are always initially blank in Rails, > > and this is also enforced by most browsers. This is because > > a malicious web page could get you to upload an arbitrary file > > by hiding the select from the user. > > > -- > > We develop, watch us RoR, in numbers too big to ignore.--~--~---------~--~----~------------~-------~--~----~ 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 Feb 4, 2008, at 5:57 PM, jasonb wrote:> Maybe I could ask this.... > > When a form is submitted with file_field.... what is the object that > is generated from the file_field?..... and what is the best way to > inspect that object?This is how I do it (which I didn''t come up with all my myself) in a create action of a controller: data = params[:model].delete(''data'') # HashWithIndifferentAccess still needs the actual key type to .delete params[:dump] = "data=#{data}; filename=#{data.original_filename}; content_type=#{data.content_type}" if data.blank? flash[:error] = "No image file selected for upload" redirect_to :action => ''new'' and return end content = data.read if content.blank? flash[:error] = "Selected upload file was empty" redirect_to :action => ''new'' and return end In my case the file is intended to be an image. I think that ''data'' will be either a StringIO or File object. That might give you enough to experiment with.> For example, if I wanted to output that object with the validation- > errors, for the sake of debugging, how would I do it?That''s what I was doing with params[:dump]. In the view (layout?), I have a <%= debug(params) %> to see what''s going on. -Rob> > > On Feb 4, 7:44 pm, jasonb <jason.bower...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: >> Ok but this is after the form his submitted and returned with errors >> >> Can anyone advise of a way to get around this? >> >> I''m not sure of the nature of the object returned by file_field.... >> does it contain the file path? am I able to re-populate the file >> field based on that object? >> >> On Feb 1, 5:57 am, Mark Reginald James <m...-bzGI/hKkdgQnC9Muvcwxkw@public.gmane.org> wrote: >> >>> jasonb wrote: >>>> In my view, I''m using <%= f.file_field :file %> >> >>>> Problems is, if the form has a validation error, this field is >>>> cleared >>>> - and the user has to reselect a file >> >>>> Does anyone have a solution for this? >> >>> HTML file select controls are always initially blank in Rails, >>> and this is also enforced by most browsers. This is because >>> a malicious web page could get you to upload an arbitrary file >>> by hiding the select from the user. >> >>> -- >>> We develop, watch us RoR, in numbers too big to ignore.Rob Biedenharn http://agileconsultingllc.com Rob-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org --~--~---------~--~----~------------~-------~--~----~ 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 Rob, much appreciated On Feb 5, 10:29 am, Rob Biedenharn <R...-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org> wrote:> On Feb 4, 2008, at 5:57 PM, jasonb wrote:> Maybe I could ask this.... > > > When a form is submitted with file_field.... what is the object that > > is generated from the file_field?..... and what is the best way to > > inspect that object? > > This is how I do it (which I didn''t come up with all my myself) in a > create action of a controller: > > data = params[:model].delete(''data'') # HashWithIndifferentAccess > still needs the actual key type to .delete > > params[:dump] = "data=#{data}; > filename=#{data.original_filename}; content_type=#{data.content_type}" > > if data.blank? > flash[:error] = "No image file selected for upload" > redirect_to :action => ''new'' and return > end > content = data.read > if content.blank? > flash[:error] = "Selected upload file was empty" > redirect_to :action => ''new'' and return > end > > In my case the file is intended to be an image. I think that ''data'' > will be either a StringIO or File object. > > That might give you enough to experiment with. > > > For example, if I wanted to output that object with the validation- > > errors, for the sake of debugging, how would I do it? > > That''s what I was doing with params[:dump]. In the view (layout?), I > have a <%= debug(params) %> to see what''s going on. > > -Rob > > > > > > > On Feb 4, 7:44 pm, jasonb <jason.bower...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > >> Ok but this is after the form his submitted and returned with errors > > >> Can anyone advise of a way to get around this? > > >> I''m not sure of the nature of the object returned by file_field.... > >> does it contain the file path? am I able to re-populate the file > >> field based on that object? > > >> On Feb 1, 5:57 am, Mark Reginald James <m...-bzGI/hKkdgQnC9Muvcwxkw@public.gmane.org> wrote: > > >>> jasonb wrote: > >>>> In my view, I''m using <%= f.file_field :file %> > > >>>> Problems is, if the form has a validation error, this field is > >>>> cleared > >>>> - and the user has to reselect a file > > >>>> Does anyone have a solution for this? > > >>> HTML file select controls are always initially blank in Rails, > >>> and this is also enforced by most browsers. This is because > >>> a malicious web page could get you to upload an arbitrary file > >>> by hiding the select from the user. > > >>> -- > >>> We develop, watch us RoR, in numbers too big to ignore. > > Rob Biedenharn http://agileconsultingllc.com > R...-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---