Forum: Ruby on Rails validating presence of file uploads

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.
5a992f256f765f8ee3dbb2af9d3db4e0?d=identicon&s=25 Robbie Shepherd (robbie)
on 2006-02-06 00:35
not sure whats going on here, but I'm using some pretty standard code to
manage file uploads in to a db, but having trouble detecting if the file
upload field has been filled or not.

in my Asset Model, I have:

def asset=(asset_field)
		self.name = base_part_of(asset_field.original_filename)

		self.content_type = asset_field.content_type.chomp
		self.data = asset_field.read
	end

	def base_part_of(file_name)
		#if !file_name

		#end
		name = File.basename(file_name)

		name.gsub(/[^\w._-]/, '')
	end

and my form has an file input field like:

<%= file_field("asset", "asset") %>


If I put in my model validates_presence_of :asset, then the upload
doesnt create a new record, even if I specify a file, so somethings not
working...any clues or workarounds?
This topic is locked and can not be replied to.