开发者

Rails Paperclip plugin - Validations

The rails plugin paperclip supports validations at the model such as:

validates_attachment_size

The only problem is that using this validation seems to force the validation of an actual attachment, where sometimes there may not be one.

If I'm validating the following, what condition :if could I use to ignore the validation if there is not :document present? (meaning the user submitted the parent object without a document a开发者_如何学JAVAttached).

validates_attachment_size :document, :less_than => 5.megabytes, :if => ???

The parent object is a :note, so in the note.rb file I have:

has_attached_file :document

RDocs: dev.thoughtbot.com/paperclip/


You can pass in :if => lambda { avatar.dirty? } after the validation statement, assuming your attachment is named avatar. For example:

validates_attachment_size :avatar, :less_than => 500.kilobytes, :if => lambda { avatar.dirty? }


You can add the :allow_nil => true option which will skip the validation if the attachment isn't present.


Check for a file name.

validates_attachment_size :document, :less_than => 5.megabytes, :if => !self.document_file_name.nil?
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜