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"> </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 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>" -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
petermichaux-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2006-Sep-02 16:22 UTC
Re: 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"> </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> 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>" > > > -- > Posted via http://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---