Getting file from form

hey,
i have this code to get the file from the form

<%= start_form_tag ( {:action => :update, :id => @fsdocument},
:multipart => true ) %>

<%= error_messages_for ‘fsdocument’ %>

<%=_ 'File' %>   <%= file_field ("file","file", 'class' => 'form_file') %>
<%= end_form_tag %>

in my controller i do this to get the file (action update)
@fsdocument = Fsdocument.find(params[:id])
file = params[:file][:file]

Now my question is:
I only need to update the file when there is one given. When the user
only changes another attribute (like name, language) and not the file,
the file must stay the same.

How can I check that the “file” parameter is a valid file, or just
empty.
When I submit nothing (must take the old file), and print it to the
console I get the following:

“#StringIO:0x346f208

Nick B. wrote:

  <td width="150" height="18" class="label_required" align="right"><%=_

file = params[:file][:file]

Now my question is:
I only need to update the file when there is one given. When the user
only changes another attribute (like name, language) and not the file,
the file must stay the same.

How about the following in your controller?

def update

@fsdocument = Fsdocument.find(params[:id])
params[:file].delete(:file) if params[:file][:file].blank?

if @fsdocument.update_attributes(params[:file])
redirect_to #…
end

end

I’m not totally sure about the “params[:file].delete(:file)” part. If
you can use the Hash#delete method on the results of params[:file].
Give it a shot.

You want to do the validation of the file in the model. Maybe post the
relavent parts of how you save the file to the db or file system and
someone can help you on that. I looked at how I validate files but it
is very specific to my other model code.

Peter