FileColumn - Validate Before Saving

file_column is great – but I need a way to run through the validations
(ie: validates_file_format_of) before saving. That is, when a user first
uploads an image it is displayed in the view but not ‘saved’ until they
click the ‘Save’ button on the form. I want to display an error
message/disable the ‘Save’ button if the file they just uploaded fails
basic validations (not correct file format, filesize too large). I’m
just not sure how to access/run-thru validations before calling
object.save.

Any ideas?
Adam

I have done a very similar thing to this, allowing people to upload an
image
from either their hard drive or an image from a url, I used file column
to
handle the validations on both, but I have to do some tweaks on the URL
image before I save it to the file column, when you upload any file to a
cgi
server it is kept in the servers temp dir until you do something with
it,
knowing this you can tweak hat file until you need it

so one trick could be to upload your own temp img, without using
file_column
for the upload, validate it then copy it to the file column if it
validates
(file column does this but you want access to ‘your’ own temp file)

to do this don’t use the file_column helper to upload, just use the
basic
rails helper or just write it in html

this is what file_column does in the background, so do something similar
for
the field ‘img_name’ in the model ‘tile’

this is a bit of a hack, but create another column in your img db
table called img2 or something and use this for storing the temp image

e.g

you need to do this because as soon as FileColumn sees anything
initilised
to its column img_name it will grab it and run with it…

then in your upload action, create an instance with the params

@tile = Tile.new(params[:tile])

@tile.img_name2 should be pointing to the temp img

validate the temp img, as you please, it is stored as a TempFile in the
servers file systems temp directory

if it all checks out…copy the temp image to the file_column using the
system commands in FileUtils, add require ‘fileutils’ to your rails app
to
use this

I can give you a better example later on when I get home, I was doing a
similar sort of thing last night and did not commit the code to my svn
so
cant have a look at it right now…

hope this gets you going in the right direction

dion

www.blogsaic.com

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs