Welcome to Working With Rails

 

Discussion Forums

Discuss all things Ruby on Rails with perhaps the web's most vibrant group of Ruby on Rails enthusiasts.
validates_attachment_presence not working correctly
3 Posts
validates_attachment_presence not working correctly

Hi,

I'm using paperclip to upload files to my server, that's working, but I'd like to add validation. I've used seen the railscast episode and Ryan mentioned that there is a way to validate the presence of a an attachment, but that isn't working. When I hit submit, the script inserts empty values in my database.

Is there a way to validate the presence of the attachment?

Thanks

http://pastie.org/689032

I've seen this behavior, although I don't entirely understand why paperclip works this way. If you create a model with an attachment, but provide no value for that attachment, then the validation will pass. However, if you create a model with an attachment and set the attachment to nil, the validation will fail. For example:

Model.new.valid? #=> true
Model.new(:photo => nil).valid? #=> false

I believe this may have to do with the way paperclip creates a default URL for missing assets (something like /publc/assets/original/missing.png), although it definitely seems like a bug to me. I haven't used paperclip in a while, so I have immediately idea how to get around this other than writing a custom validation that digs around in the paperclip Attachment object.

I've find a way to use the validation rules given. There seems to be an update of the package (paperclip) and with that update, validation is fixed.

Thanks

3 Posts
Login to add your message