gpt4 book ai didi

ruby-on-rails - Carrierwave:如何验证图像是否已附加? (上传或remote_url)

转载 作者:行者123 更新时间:2023-12-03 15:56:12 25 4
gpt4 key购买 nike

我试图确保图片模型的每个实例都附加了一个文件。

换句话说 - 表单中有两个字段:

:file
:remote_file_url

我希望用户至少填写其中之一。

当我验证 :file 的存在并提交 remote_file_url 而不是文件时 - 那么它会给出验证错误。

我现在找到的唯一方法是这样做:
class Picture < ActiveRecord::Base
validate :file_xor_remote_file_url

private
def file_xor_remote_file_url
if !(file.blank? ^ remote_file_url.blank?)
errors.add(:base, "Specify a file to upload, or file URL, not both")
end
end

end

最佳答案

The wiki说你可以像这样验证你的上传:

mount_uploader :avatar, AvatarUploader
validates_presence_of :avatar

它没有提到对远程 URL 的任何不同处理,但强调 Carrierwave 验证文件的存在,而不仅仅是 URL 的存在。因此,给定的远程 URL 必须引用可以上传的有效文件。

你说“验证 :file 的存在”,但也许这可能是重点。希望这可以帮助。

关于ruby-on-rails - Carrierwave:如何验证图像是否已附加? (上传或remote_url),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19411129/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com