Hi! Question... I''m storing a file in my database using the method described in the AWDWR book (see page 362). My code looks like this: class Myfile < ActiveRecord::Base belongs_to :folder validates_uniqueness_of :name, :scope => "folder_id" validates_presence_of :name #see book pp. 362 def myfile=(myfile_field) self.name = base_part_of(myfile_field.original_filename) if myfile_field != "" self.data = myfile_field.read if myfile_field != "" end def base_part_of(file_name) name = File.basename(file_name) name.gsub(/[^\w._-]/, "") end end Basically I''m doing exactly the same as in the book. The only difference is I''m checking if there actually is a file like this: self.name = base_part_of(myfile_field.original_filename) if myfile_field != "" self.data = myfile_field.read if myfile_field != "" First I tried this checking if there is a file by checking if myfile_field != nil, but this doesn''t work. I was wondering why myfile_field is empty instead of nil when there''s no file. Also, I''d like to know if my way of checking the myfile_field is the "right" way. Thanks, Mischa.
Hi, You can try to check the size of the file: if myfile_field.size == 0 But I''m not sure.. 2005/11/2, Mischa Berger:> Hi! > > Question... > > I''m storing a file in my database using the method described in the > AWDWR book (see page 362). > > My code looks like this: > > > class Myfile < ActiveRecord::Base > belongs_to :folder > validates_uniqueness_of :name, :scope => "folder_id" > validates_presence_of :name > > #see book pp. 362 > def myfile=(myfile_field) > self.name = base_part_of(myfile_field.original_filename) if > myfile_field != "" > self.data = myfile_field.read if myfile_field != "" > end > def base_part_of(file_name) > name = File.basename(file_name) > name.gsub(/[^\w._-]/, "") > end > end > > Basically I''m doing exactly the same as in the book. The only difference > is I''m checking if there actually is a file like this: > > self.name = base_part_of(myfile_field.original_filename) if myfile_field > != "" > self.data = myfile_field.read if myfile_field != "" > > First I tried this checking if there is a file by checking if > myfile_field != nil, but this doesn''t work. I was wondering why > myfile_field is empty instead of nil when there''s no file. Also, I''d > like to know if my way of checking the myfile_field is the "right" way. > > Thanks, > Mischa. > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
> if myfile_field.size == 0Thanks, I like your method. My stuff looks like this now: def myfile=(myfile_field) if myfile_field.size > 0 self.filename = base_part_of(myfile_field.original_filename) self.data = myfile_field.read end end I still wonder why myfile_field is not nil if you do not select a file. My view looks like this: <label for="myfile_name">Filename</label><br/> <%= file_field ''myfile'', ''myfile'' %> <%= hidden_field ''myfile'', ''folder_id'' %> My controller looks like this: def new @myfile = Myfile.new id = @params[:id] @myfile.folder = Folder.find(id) if id != nil end def create @myfile = Myfile.new(@params[:myfile]) if @myfile.save flash[''notice''] = ''Myfile was successfully created.'' redirect_to :controller => ''folder'', :action => ''list'', :id => @params[:myfile][:folder_id] else render_action ''new'' end end Any ideas? Grt, Mischa.
I always check like that if myfile and myfile.size > 0 # do upload stuff end so you''re on the safe side! However it would be nice if rails filtered out files with length = 0 On Sat, 05 Nov 2005 11:04:49 +0100, Mischa Berger <spmm_pls-/E1597aS9LQAvxtiuMwx3w@public.gmane.org> wrote:>> if myfile_field.size == 0 > Thanks, I like your method. My stuff looks like this now: > > def myfile=(myfile_field) > if myfile_field.size > 0 > self.filename = base_part_of(myfile_field.original_filename) > self.data = myfile_field.read > end > end > > I still wonder why myfile_field is not nil if you do not select a file. > > My view looks like this: > > <label for="myfile_name">Filename</label><br/> > <%= file_field ''myfile'', ''myfile'' %> > <%= hidden_field ''myfile'', ''folder_id'' %> > > My controller looks like this: > > def new > @myfile = Myfile.new > id = @params[:id] > @myfile.folder = Folder.find(id) if id != nil > end > > def create > @myfile = Myfile.new(@params[:myfile]) > if @myfile.save > flash[''notice''] = ''Myfile was successfully created.'' > redirect_to :controller => ''folder'', :action => ''list'', :id => > @params[:myfile][:folder_id] > else > render_action ''new'' > end > end > > Any ideas? > > Grt, > Mischa. > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
> I always check like that > > if myfile and myfile.size > 0 > # do upload stuff > end > > so you''re on the safe side! > > However it would be nice if rails filtered out files with length = 0Thanks! I have another (slightly related) question. I want to display the size of the files I stored in my MySQL database blob field. I tried this: <%= myfile.data.size %> But that results for every file in: 65535. Do I have to store the size in a seperate field when I''m storing the file in the database or is it somehow possible to get the size of the file stored in myfile.data?? Grt, Mischa.
Eventually it''s the datatype you are using... I use ''MEDIUMBLOB'' in mysql and must say that something like up to 16MB or so ... <%= @image.data.size %> renders the right size On Sat, 05 Nov 2005 13:14:45 +0100, Mischa Berger <spmm_pls-/E1597aS9LQAvxtiuMwx3w@public.gmane.org> wrote:>> I always check like that >> if myfile and myfile.size > 0 >> # do upload stuff >> end >> so you''re on the safe side! >> However it would be nice if rails filtered out files with length = 0 > Thanks! > > I have another (slightly related) question. I want to display the size > of the files I stored in my MySQL database blob field. I tried this: <%= > myfile.data.size %> But that results for every file in: 65535. Do I have > to store the size in a seperate field when I''m storing the file in the > database or is it somehow possible to get the size of the file stored in > myfile.data?? > > Grt, > Mischa. > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >