英文:
Why does ActiveStorage 7 in Rails include a hidden field by default?
问题
I recently came across an issue in my Rails application where an empty string was always being included in an array of attached files even if no files had been uploaded.
在我的Rails应用程序中,我最近遇到一个问题,即使没有上传任何文件,空字符串始终会包含在附加文件的数组中。
This resulted in the attached_files
param always being passed to the controller and showing an array with an empty string [""]
if no files had been uploaded.
这导致attached_files
参数始终传递到控制器,并在没有上传文件的情况下显示一个包含空字符串的数组[""]
。
I got some help for my problem with this response, but my question is, in what scenario would you need to handle an empty collection with the default hidden field? Why is it useful to have it as the default behaviour?
我通过这个回答得到了一些关于我的问题的帮助,但我的问题是,在什么情况下你需要使用默认隐藏字段来处理空集合?为什么将其设置为默认行为很有用?
英文:
I recently came across an issue in my Rails application where an empty string was always being included in an array of attached files even if no files had been uploaded.
This resulted in the attached_files
param always being passed to the controller and showing an array with an empty string [""]
if no files had been uploaded.
I got some help for my problem with this response, but my question is, in what scenario would you need to handle an empty collection with the default hidden field? Why is it useful to have it as the default behaviour?
答案1
得分: 1
这就像为复选框创建了一个隐藏的空字段一样。这是因为如果根本没有字段,那么根据HTML/CGI标准,浏览器不会向服务器发送任何参数。因此,将无法删除所有附件。
英文:
It's like the way there's a hidden empty field created for a checkbox. It's done because if there's no field at all, then the HTML/CGI standard means that be no param is sent by the browser to the server. So there'd be no way to delete all attachments.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论