require an attachment using paperclip

23 views
Skip to first unread message

hobo_hippy

unread,
Jun 29, 2011, 7:45:27 PM6/29/11
to Hobo Users
So it's pretty clear you can require a field like title below:


fields do
timestamps
title :string, :required
event :string
end

but what if I wanted to require an attachment using paperclip-with-
hobo?

I already have an photo attachment setup as below:

has_attached_file :photo, :default_url => "/images/blah.jpg"


but I have no idea how to REQUIRE that a user uploads an image. Any
ideas? possible solutions?

Henry Baragar

unread,
Jun 29, 2011, 8:12:42 PM6/29/11
to hobo...@googlegroups.com

validates_presence_of :photo


But, this does not guarantee that what is uploaded actually is an image.


Henry


--

Henry Baragar

Instantiated Software


Matt Jones

unread,
Jun 29, 2011, 8:19:50 PM6/29/11
to hobo...@googlegroups.com

:required on a field is really just a shorthand for validates_presence_of (a standard AR validation). There are some attachment-specific validations in Paperclip, but they apparently aren't cool enough to mention in the README. Here's the rdoc:

http://rdoc.info/gems/paperclip/2.3.12/Paperclip/ClassMethods

--Matt Jones


kevinpfromnm

unread,
Jun 30, 2011, 12:50:21 AM6/30/11
to hobo...@googlegroups.com
validates_attachment_content_type :image, :content_type => /image\//

hobo_hippy

unread,
Jun 30, 2011, 8:31:07 PM6/30/11
to Hobo Users
COOL!~

thanks for the link to the rdoc

-HoboHippy

hobo_hippy

unread,
Jul 5, 2011, 11:33:01 PM7/5/11
to Hobo Users
actually i tried the validates_presence_of :photo, but no luck. it
doesn't throw an error, but I can still submit a new model without
uploading a photo... weird
Reply all
Reply to author
Forward
0 new messages