ruby-on-rails - 在 Rails 6.0 升级后 ActiveStorage 没有将 blob 持久化到附件
问题描述
由于 Rails 6.0 升级(从 5.2.x 开始),我的 ActiveStorage 出现了问题。
我有一个自定义管理界面,其中页面有字段,每个字段都有一个 STItype
来创建复杂的页面布局。使用 Active Storage的Image
类型(它是 Paperclip,但在 Rails 5.2 发布后迁移)通过简单的上传模式存储图像 - 没有复杂的直接上传 JS。
类结构如下所示(为简洁起见,我删除了额外的代码):
class Page < ApplicationRecord
has_many :fields, dependent: :destroy
accepts_nested_attributes_for :fields, allow_destroy: true
end
class Field < ApplicationRecord
belongs_to :page
end
module Fields
class Image < Content::Field
has_one_attached :image
accepts_nested_attributes_for :image_attachment, allow_destroy: true
end
end
当页面更新时,它是通过嵌套表单属性和一个简单的调用来实现的@page.update(permitted_params)
。
(permitted_params
减去不重要的参数),看起来像这样......
{
"fields_attributes"=> {
"0"=> {
"type"=>"Fields::Image",
"image"=>#<ActionDispatch::Http::UploadedFile:0x00007ff36cdd9908 @tempfile=#<Tempfile:/var/folders/_t/clkdb5ms365617_039m7y7sc0000gn/T/RackMultipart20191021-38488-4kyp8k.jpg>, @original_filename="image.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"content_page[fields_attributes][0][image]\"; filename=\"image.jpg\"\r\nContent-Type: image/jpeg\r\n">,
"id"=>"8059"
}
}
}
为了保持 ActiveStorage 5.2 更新集合的行为,而不是覆盖它,我在我的application.rb
:
config.active_storage.replace_on_assign_to_many = false
但是,因为字段关系是 a has_one_attached
,所以这不应该影响它。
从日志中,没有开始上传,也没有对 Active Storage 表进行更新,但将一个更新写入Page
表:
Content::Page Update (5.2ms) UPDATE `pages` SET `pages`.`updated_at` = '2019-10-21 13:58:18' WHERE `pages`.`id` = 105 /*application:myapp,controller:pages,action:update*/
仅此而已,仅此而已。
但是,如果我byebug
在更新方法中然后手动执行更新:
$ img = Fields::Image.new(image: permitted_params.dig("fields_attributes", "0", "image"))
#<Content::Fields::Image id: nil, type: "Content::Fields::Image">
$ img.valid?
# true
$ img.save
(4.6ms) BEGIN
Field Load (1.5ms) SELECT `fields`.* FROM `fields` WHERE `fields`.`page_id` IS NULL AND (`fields`.`position` IS NOT NULL) ORDER BY `fields`.`position` DESC LIMIT 1
Fields::Image Create (1.7ms) INSERT INTO `fields` (`type`) VALUES ('Fields::Image')
ActiveStorage::Blob Load (1.6ms) SELECT `active_storage_blobs`.* FROM `active_storage_blobs` INNER JOIN `active_storage_attachments` ON `active_storage_blobs`.`id` = `active_storage_attachments`.`blob_id` WHERE `active_storage_attachments`.`record_id` = 8062 AND `active_storage_attachments`.`record_type` = 'Fields::Image' AND `active_storage_attachments`.`name` = 'image' ORDER BY `active_storage_attachments`.`position` ASC LIMIT 1
ActiveStorage::Attachment Load (1.6ms) SELECT `active_storage_attachments`.* FROM `active_storage_attachments` WHERE `active_storage_attachments`.`record_id` = 8062 AND `active_storage_attachments`.`record_type` = 'Fields::Image' AND `active_storage_attachments`.`name` = 'image' ORDER BY `active_storage_attachments`.`position` ASC LIMIT 1
ActiveStorage::Blob Create (2.7ms) INSERT INTO `active_storage_blobs` (`key`, `filename`) VALUES ('mnf9brbh9e1kyriummf11kpd7t6q', 'image.jpg')
ActiveStorage::Attachment Load (2.0ms) SELECT `active_storage_attachments`.* FROM `active_storage_attachments` WHERE `active_storage_attachments`.`record_type` = 'Fields::Image' AND `active_storage_attachments`.`name` = 'image' AND `active_storage_attachments`.`record_id` = 8062 AND (`active_storage_attachments`.`position` IS NOT NULL) ORDER BY `active_storage_attachments`.`position` DESC LIMIT 1
ActiveStorage::Attachment Create (4.1ms) INSERT INTO `active_storage_attachments` (`name`, `record_type`, `record_id`) VALUES ('image', 'Fields::Image')
Fields::Image Update (2.3ms) UPDATE `fields` SET `fields`.`updated_at` = '2019-10-21 14:24:06' WHERE `fields`.`id` = 8062
(2.0ms) COMMIT
FlatDisk Storage (5.3ms) Uploaded file to key: mnf9brbh9e1kyriummf11kpd7t6q (checksum: ms9Zu1oagR5kGjcci1YfhQ==)
[ActiveJob] Enqueued ActiveStorage::AnalyzeJob (Job ID: 034d495a-01c1-4935-a873-b07fcec35f47) to Sidekiq(active_storage_analysis) with arguments: #<GlobalID:0x00007ff36c2e2090 @uri=#<URI::GID gid://dswt/ActiveStorage::Blob/52786>>
# true
所以这让我认为这可能与嵌套参数有关,但我看不出这些有问题,所以我对可能发生的事情感到困惑。
图片上传在网站的其他地方正常运行,只是在这个地方他们没有按预期工作。
任何帮助将不胜感激。
谢谢你,保罗。
编辑:
我已将其缩小到 ActiveStorage 不更新图像关联,除非模型上的字段(updated_at
或表中的任何列)也被更新。这似乎触发了 ActiveRecord 来持久化实例,从而触发 ActiveStorage 上传文件。
解决方案
在玩了一堆之后,我发现除非模型以某种方式更改,否则 Active Storage 关系不会被更新,例如,触摸updated_at
时间戳,或者另一个编辑另一个字段 - 我认为这是一个 Rails 错误。
通过添加<%= f.hidden_field :updated_at, value: DateTime.current %>
我的表单,我能够强制 Active Storage 保存文件。
希望这对其他人有帮助。
推荐阅读
- c - struct Node *ptr = malloc(sizeof(*ptr)) 是如何工作的?
- java - IntelliJ Java Profiler 过滤器类
- c# - 修复这不是一个有效的位图文件
- flutter - 如何通过我的 Flutter App 访问 Postgresql 中的用户定义过程
- kubernetes - 与 kubectl 一起使用时,mobaxterm 无法读取 kube 配置设置?
- java - 如何使用 log4j 在日志文件中记录所有 SQL 查询?
- docker - Ansible 无法在 awx_task 容器中找到拉取的文件,尽管它在那里
- pandas - 在地图上标绘车站
- docker - 为什么nsenter不能列出容器进程信息
- php - 分组数组php和计数