所以,我有一个带有JS旋转的滑块标题图像的应用程序。现在,我已经为管理员提供了选择上传滑块将显示在哪个页面上的选项,他们可以在着陆页和索引页之间进行选择。
以下是我的滑块模型的外观:
class Slider < ActiveRecord::Base
validates :short_text, presence: true
has_attached_file :image, styles: {
medium: '800x>',
small: '300x>'
}
validates_attachment_content_type :image, :content_type => /\Aimage\/.*\Z/
validates :image, dimensions: { width: 1024, height: 427 }, on: :create
SLIDER_TYPES = {
0 => "Both (Landing and Index page)",
1 => "Index page",
2 => 'Landing page'
}
end
问题是,我想让管理员为索引页面上传更大的图片,并保留当前维度验证,即width: 1024, height: 427
仅用于着陆页。
是否可以根据我的上述代码为不同的页面设置不同的验证?
这里的任何帮助都会很棒!
谢谢!
答案 0 :(得分:0)
您可以在方法中创建custom validation methods
和,您可以检查您的slider_type,然后相应地验证其尺寸,例如:
class Slider < ActiveRecord::Base
validate :check_for_dimension
def check_for_dimension
# your logic to check type and apply dimension accordingly
end
end