Forum: Ruby on Rails file upload validations?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
81194a50c0f9bd95d7832a77fdf371bd?d=identicon&s=25 cool_screen_name90001 (Guest)
on 2005-11-17 10:26
(Received via mailing list)
It appears there are no validation class methods that can be used on
file uploads? These would be
nice :)

validates_file_type
validates_file_size


csn



__________________________________
Yahoo! FareChase: Search multiple travel sites in one click.
http://farechase.yahoo.com
C7669e8b5676f61fdf202230cbcf72d8?d=identicon&s=25 Mikkel Bruun (mikkel)
on 2005-11-17 10:54
cool_screen_name90001 wrote:
> It appears there are no validation class methods that can be used on
> file uploads? These would be
> nice :)
>
> 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
81194a50c0f9bd95d7832a77fdf371bd?d=identicon&s=25 cool_screen_name90001 (Guest)
on 2005-11-18 04:26
(Received via mailing list)
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 Bruun <mikkel@helenius.dk> wrote:

> >
> But it would be an excellent code snippet...
>
> Mikkel
>
> --
> Posted via http://www.ruby-forum.com/.
> _______________________________________________
> Rails mailing list
> Rails@lists.rubyonrails.org
> http://lists.rubyonrails.org/mailman/listinfo/rails
>




__________________________________
Yahoo! FareChase: Search multiple travel sites in one click.
http://farechase.yahoo.com
This topic is locked and can not be replied to.