Displaying errors on a Paperclip file field

442 views
Skip to first unread message

Andrew Stewart

unread,
Jan 19, 2011, 8:24:33 AM1/19/11
to SimpleForm
Hello,

I would appreciate help figuring out how to display any validation
errors from Paperclip when a user tries to upload an invalid file.
The problem is that the attribute SimpleForm uses for the file field
does not match any of the actual columns/attributes managed by
Paperclip.

For example, here is my model:

class Document < ActiveRecord::Base
has_attached_file :attachment,
validates_attachment_presence :attachment
validates_attachment_size :attachment, :less_than =>
10.megabytes
end

And here is my form:

= simple_form_for @document, :html => {:multipart => true} do |f|
= f.error_notification
= f.input :title
= f.input :attachment, :as => :file, :required => true
= f.submit

I would like SimpleForm to display on the :attachment field any/all
errors that Paperclip generates. So if the user doesn't upload any
file, or uploads one which is too large, SimpleForm will render the
message(s) in the :attachment field's error span.

The Paperclip errors in this case are keyed on `attachment_file_name`
(to validate the presence) and `attachment_file_size`.

Is there a way to pass a regexp to SimpleForm's input method to tell
it which attributes' errors to gather up? E.g.:

= f.input :attachment, :as => :file, :required => true, :errors_on
=> /attachment/

That would be super helpful!

Many thanks,

Andy Stewart
----
http://airbladesoftware.com

Carlos Antonio da Silva

unread,
Jan 19, 2011, 8:54:37 AM1/19/11
to plataformate...@googlegroups.com
Nope, but you should be able to use f.error with the other attributes you need, right below the f.input call. This should give you the expected result.
--
At.
Carlos A. da Silva
Reply all
Reply to author
Forward
0 new messages