File upload field empty when validation error

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

Does anyone have any ideas?

Is it even possible to fix?

Have been googling, but no luck

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.

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?

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?

Thanks Rob, much appreciated

On Feb 5, 10:29 am, Rob B. [email protected]

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

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 B. http://agileconsultingllc.com
[email protected]