Getting file from form

Nick Brutyn wrote:

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' %>

<table width="100%" border="0" cellpadding="0" cellspacing="5">
  <tr>
    <td width="150" height="18" class="label_required" align="right"><%=_
'File' %></td>
    <td width="10" height="18">&nbsp;</td>
    <td height="18"><%= file_field ("file","file", 'class' => 'form_file')
%></td>
  </tr>
</table>
<%= 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 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