历史记录:
How to display a presence validation conditionally in combination with carrierwave?
我正在用这个宝石构建一个对象:
https://github.com/schneems/wicked/wiki/Building-Partial-Objects-Step-by-Step
validates :name, :presence => true, :if => :active_or_name?
validates :details, :presence => true, :if => :active_or_details?
validates :image, presence: true, image_size: { width: { min: 400 },
height: { min: 400 } }, :file_size => { :maximum => 5.megabytes.to_i },
if: :active_or_details?
具有以下功能:
def active?
status == 'active'
end
def active_or_name?
status.include?('name') || active?
end
def active_or_details?
status.include?('details') || active?
end
我如何设置状态:
params[:item][:status] = step.to_s
params[:item][:status] = 'active' if step == steps.last
最后一步是详细信息页面后的2步,因此它不应处于“活动”状态。
在历史记录(上面的链接)中,您可以看到我应该将以下内容添加到
if:
图像验证的:
-> (record) { record.image_integrity_error.blank? }
我试着用
validates :image, presence: true, image_size: { width: { min: 400 },
height: { min: 400 } }, :file_size => { :maximum => 5.megabytes.to_i },
if: :active_or_details? && -> (record) { record.image_integrity_error.blank? }
这会导致页面上的验证仅显示
Image You are not allowed to upload "txt" files, allowed types: jpg, jpeg, gif, png
但现在,验证将在之前需要的提交步骤的每一页上进行
Image can't be blank
.
我试图重构
-> (record) { record.image_integrity_error.blank? }
进入
active_or_details?
使用if/else语句。然后
图像不能为空
在详细信息页面上返回。
请告知。