如何正确地将record.image_integrity_error添加到现有条件?
历史: 如何与carrierwave结合有条件地显示状态validation?
我正在使用这个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?
function:
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
最后一步是在详细信息页面之后的两个步骤,因此它不应该是“活动的”。
在历史记录(上面的链接)中,您可以读到我应该将以下内容添加到if:
of 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? }
这导致页面上的validation仅显示Image You are not allowed to upload "txt" files, allowed types: jpg, jpeg, gif, png
但现在validation发生在之前所需的提交步骤的每一页上,而Image can't be blank
。
我试着重构-> (record) { record.image_integrity_error.blank? }
进入active_or_details?
使用if / else语句。 然后在详细信息页面上返回Image can't be blank
。
请指教。
根据评论@engineersmnky: if
键控条件可以作为Array
提交,例如if: [:active_or_details?,-> (record) { record.image_integrity_error.blank? }]
if: [:active_or_details?,-> (record) { record.image_integrity_error.blank? }]