Ruby on rails 回形针覆盖验证子类中的_附件
我试图覆盖Ruby on rails 回形针覆盖验证子类中的_附件,ruby-on-rails,paperclip,paperclip-validation,Ruby On Rails,Paperclip,Paperclip Validation,我试图覆盖子类中的验证附件,但我注意到它只适用于超类验证;我想知道为什么我的验证子类中的\u附件不起作用。有人面对过这个问题吗?你是如何解决这个问题的?下面是一个示例代码: class Superclass validates_attachment :logo, :image_ratio => { :ratio => {"1:1" => "28", "4:1" => "50", "5:1" => "40"} } end class Subclass &l
子类
中的验证附件
,但我注意到它只适用于超类
验证;我想知道为什么我的验证子类中的\u附件不起作用。有人面对过这个问题吗?你是如何解决这个问题的?下面是一个示例代码:
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
类超类
验证附件:徽标,:图像\u比率=>{:比率=>{“1:1”=>“28”,“4:1”=>“50”,“5:1”=>“40”}
结束
类子类<超类
验证附件:徽标,:图像\u比率=>{:比率=>{“1:1”=>“40”,“2:1”=>“60”}
结束
我建议您将类的两个字段放在不同的表中。你可能因此而遇到问题
但是,如果您真的希望两个类都只有一个表,那么我相信您可以使用如下内容:
validates_attachment :logo, :image_ratio => { :ratio => {"1:1" => "40", "2:1" => "60"} }, :unless => Proc.new {|attach| attach.type == "SubClass"}
我假设您有一个附件类型
列,但取决于您如何确定附件类型是否为子类
,您可以自行更改它
您还可以尝试从子类
中删除验证附件
,而在模型中尝试使用选项
,如下所示:
with_options :unless => :attach_type == "SubClass" do |attach|
attach.validates_attachment :logo, :image_ratio => { :ratio => {"1:1" => "40", "2:1" => "60"}}
end
这对我有用。。。轨道4
validates :photo, :presence => true,
:attachment_content_type => { :content_type => "image/jpg" },
:attachment_size => { :in => 0..10.kilobytes }
如果其他任何人遇到问题,需要在验证前访问实例,我使用了以下方法:
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
class AttachmentDynamicContentTypeValidator
在实际的资产实例中,我添加了以下内容:
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
class资产
希望这能帮助别人 您收到了什么错误消息?是不是未定义的方法验证了\u附件
?请告诉我您使用的是哪种版本的回形针?我使用的是回形针(3.1.4)。事实上,并没有错误消息,但验证在超类中执行,而不是在子类中执行。您是否将这两个类放在同一个表中?我放在同一个表中。我在附件中使用多态性概念。