File upload validations?

It appears there are no validation class methods that can be used on
file uploads? These would be
nice :slight_smile:

validates_file_type
validates_file_size

csn


Yahoo! FareChase: Search multiple travel sites in one click.
http://farechase.yahoo.com

cool_screen_name90001 wrote:

It appears there are no validation class methods that can be used on
file uploads? These would be
nice :slight_smile:

validates_file_type
validates_file_size

csn


Yahoo! FareChase: Search multiple travel sites in one click.
http://farechase.yahoo.com

Those validations would only make sense on a UploadedFileClass…As this
class doesnt exsist in rails, there’s no need for these validations in
the core…

But it would be an excellent code snippet…

Mikkel

Turns out it’s pretty easy.

In item’s create view:

<%= file_field ‘item’, ‘file’ %>

and item’s model:

class Item < ActiveRecord::Base
validate :check_file_size

def check_file_size
	if @file and @file.size > 20000
		errors.add('file', 'too big')
	end
end

def file
	@file
end

def file=(file)
	@file=file
end

end

csn

— Mikkel B. [email protected] wrote:

But it would be an excellent code snippet…

Mikkel

–
Posted via http://www.ruby-forum.com/.


Rails mailing list
[email protected]
http://lists.rubyonrails.org/mailman/listinfo/rails


Yahoo! FareChase: Search multiple travel sites in one click.
http://farechase.yahoo.com