gpt4 book ai didi

ruby-on-rails - 使事件存储 has_one_attatched 不为空

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

我有一个使用 has_one_attached 设置事件存储的模型,一些用户在没有向表单添加文件的情​​况下点击了提交。在我向前端添加验证之前,我想确保后端将拒绝没有附件的记录。

我如何设置一个模型,除非事件存储 has_one_attached 中有东西,否则它不会保存?

最佳答案

我认为,目前唯一的选择是遵循“常规”路径:将 include ActiveModel::Validationsvalidates_with QwertieCustomValidator 添加到您的模型中,在 app/validators 下创建自定义验证器并在那里检查是否 record .attached_file.attached?,如果没有,添加错误:record.errors.add(:attached_file, 'no file was attached')

转念一想,你不需要在其他文件中指定验证器,我只是在我的项目中这样做以匹配改编项目的流程。最短路线如下所示:

has_one_attached :attached_file

validate :check_file_presence

def check_file_presence
errors.add(:attached_file, "no file added") unless attached_file.attached?
end

希望对你有帮助。

关于ruby-on-rails - 使事件存储 has_one_attatched 不为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53147074/

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