如何正确地将 record.image_integrity_error 添加到现有条件?
How to correctly add the record.image_integrity_error to existing condition?
历史:
我正在用这个 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步,所以不应该是'active'。
在历史记录(上面的 link)中,您可以看到我应该将以下内容添加到图像验证的 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
。
我试图用 if/else 语句将 -> (record) { record.image_integrity_error.blank? }
重构为 active_or_details?
。然后是详情页的Image can't be blank
returns.
请指教
根据@engineersmnky 的评论:if
键控条件可以作为 Array
提交,例如if: [:active_or_details?,-> (record) { record.image_integrity_error.blank? }]
历史:
我正在用这个 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步,所以不应该是'active'。
在历史记录(上面的 link)中,您可以看到我应该将以下内容添加到图像验证的 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
。
我试图用 if/else 语句将 -> (record) { record.image_integrity_error.blank? }
重构为 active_or_details?
。然后是详情页的Image can't be blank
returns.
请指教
根据@engineersmnky 的评论:if
键控条件可以作为 Array
提交,例如if: [:active_or_details?,-> (record) { record.image_integrity_error.blank? }]