0

我正在尝试覆盖validates_attachmentSubclass但我注意到它仅适用于Superclass验证;我想知道为什么我validates_attachment的子类不起作用。有人遇到过这个问题吗?你是如何解决这个问题的?这是一个示例代码:

class Superclass
    validates_attachment :logo, :image_ratio => { :ratio  => {"1:1" => "28", "4:1" => "50", "5:1" => "40"} }
end

class Subclass < Superclass
  validates_attachment :logo, :image_ratio => { :ratio  => {"1:1" => "40", "2:1" => "60"} }
end
4

3 回答 3

1

我建议您应该将两个类的字段放在不同的表中。因此,您可能会遇到问题。

但是,如果您真的只想为这两个类提供一个表,那么我相信您可以使用这样的东西:

validates_attachment :logo, :image_ratio => { :ratio  => {"1:1" => "40", "2:1" => "60"} }, :unless => Proc.new {|attach| attach.type == "SubClass"}

我假设您有一个attach_type列,但取决于您如何确定附件类型是否为 a SubClass,由您来更改它。

您也可以尝试validates_attachment从您的模型中删除Subclass,而是尝试with_options在您的模型中,如下所示:

with_options :unless => :attach_type == "SubClass" do |attach|
   attach.validates_attachment :logo, :image_ratio => { :ratio  => {"1:1" => "40", "2:1" => "60"}}
end
于 2013-02-22T09:16:26.620 回答
0

这对我有用... rails 4

  validates :photo, :presence => true,
  :attachment_content_type => { :content_type => "image/jpg" },
  :attachment_size => { :in => 0..10.kilobytes }
于 2014-02-20T21:16:05.937 回答
0

万一其他人遇到问题,他们需要实例访问才能验证我使用以下内容:

class AttachmentDynamicContentTypeValidator < Paperclip::Validators::AttachmentContentTypeValidator
   def validate_each(record, attribute, value)
      @record = record
      super
   end

   def allowed_types
      @record.my_valid_types_array || ["text/plain"]
   end

   def check_validity!; end
end

在实际的资产实例中,我添加了以下内容:

class Asset < ActiveRecord::Base
   validates :asset, attachment_dynamic_content_type: :asset_content_type
end

希望对某人有所帮助。

于 2017-05-19T10:53:54.737 回答