【问题标题】:custom paperclip validation error message自定义回形针验证错误消息
【发布时间】:2023-03-28 05:19:01
【问题描述】:

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

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)

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

谢谢

【问题讨论】:

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


    【解决方案1】:
    > 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
    

    【讨论】:

    • 请注意,您需要在验证中添加一个条件: validate :check_content_type, if: ->(){ image.present? }
    【解决方案2】:

    我参加这个聚会迟到了。

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

    得到你:

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

    "图片文件太大,尽量小于2mb"

    【讨论】:

      【解决方案3】:

      您好,请验证回形针头像

      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 }  
      

      【讨论】:

      • 什么?这没有回答问题
      • 这里没有关于 :message 的内容,无论如何它都不起作用。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-18
      • 2016-12-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多