ActiveStorage 7在Rails中默认包含一个隐藏字段的原因是什么?

huangapple go评论67阅读模式
英文:

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.

huangapple
  • 本文由 发表于 2023年7月6日 16:37:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/76626979.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定