自定义回形针验证错误消息

时间:2013-04-19 08:13:36

标签: ruby-on-rails paperclip paperclip-validation

我对内容类型进行了验证:

validates_attachment_content_type :photo, :content_type => /^image\/(jpg|jpeg|pjpeg|png|x-png|gif)$/, :message => 'file type is not allowed (only jpeg/png/gif images)'

我只希望显示上面的消息,而是显示

 Photos photo content type file type is not allowed (only jpeg/png/gif images)

因为它是照片模型和附件照片。

感谢

3 个答案:

答案 0 :(得分:2)

> Not a real solution but a Easy one is to skip paperclip validation and
> write custom one
>     validate :check_content_type
>     
>       def check_content_type
>        if !['image/jpeg', 'image/gif','image/png'].include?(self.image_content_type)
>         errors.add_to_base("File '#{self.image_file_name}' is not a valid image type") # or errors.add
>        end
>       end

答案 1 :(得分:0)

我迟到了。

validates_attachment_size :image, :in => 0.megabytes..2.megabytes,  message: " is too large, try less than 2mb  or for help"

获取你:

应该让你离家更近,输出为:

"图像文件太大,尝试少于2mb"

答案 2 :(得分:-3)

你好请验证回形针avtar图像

attr_accessible :avatar

has_attached_file :avatar, :styles => { :small => "60x60>", :thumb => "60x60>" }

validates_attachment :avatar, :presence => true, 
:content_type => { :content_type => "image/jpg" }, 
:size => { :in => 0..1000.kilobytes }