如何正确地将record.image_integrity_error添加到现有条件? [英] How to correctly add the record.image_integrity_error to existing condition?

查看:97
本文介绍了如何正确地将record.image_integrity_error添加到现有条件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

历史记录:如何与载波结合有条件地显示状态验证?

我正在使用此gem构建对象: https://github.com/schneems/wicked/wiki/Building-Partial-Objects-Step-by-Step

I'm building an object with this gem: 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个步骤,因此不应处于活动状态

The last step is 2 steps after the details page, so it should not be 'active'.

在历史记录(上面的链接)中,您可以看到我应该在中添加以下内容:图像验证的时间:

In the History (link above) you can read that I should be adding the following to the if: of the image validation:

-> (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? }

这会导致页面上的验证仅显示
图片您不允许上传 txt文件,允许的类型:jpg,jpeg,gif,png

This causes the validation on the page to show only the Image You are not allowed to upload "txt" files, allowed types: jpg, jpeg, gif, png

但是现在验证生效了图像的必填提交步骤的每一页上的图片不能为空白

But now the validation happens on each page of the before needed submission steps with Image can't be blank.

我试图重构-> (记录){record.image_integrity_error.blank? } 插入到 active_or_details?中,并带有if / else语句。然后图片不能为空白在详细信息页面上返回。

I tried to refactor the -> (record) { record.image_integrity_error.blank? } into the active_or_details? with an if/else statement. Then the Image can't be blank returns on the details page .

请告知。

推荐答案

根据评论@engineersmnky:如果关键条件可以作为<$ c提交$ c>数组例如如果:[:active_or_details?,-> (记录){record.image_integrity_error.blank? }]

As per comment @engineersmnky: if keyed conditions can be submitted as an Array e.g. if: [:active_or_details?,-> (record) { record.image_integrity_error.blank? }]

这篇关于如何正确地将record.image_integrity_error添加到现有条件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆