gpt4 book ai didi

ruby-on-rails - 在 Rails 6.0 升级后,ActiveStorage 不会将 blob 持久化到附件

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

我有一个 ActiveStorage 问题,似乎是由于 Rails 6.0 升级(从 5.2.x)引起的。

我有一个自定义管理界面,其中页面有字段,每个字段都有一个 STI type创建复杂的页面布局。 Image使用 Active Storage(它是 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
但是,因为字段关系是 has_one_attached ,这应该不会影响它。

从日志中,没有开始上传,也没有对 Active Storage 表进行更新,但有一个更新被写入 Page。 table :
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在 update 方法中,然后手动执行更新:
$ 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 上传文件。

最佳答案

经过一番折腾,我发现除非模型以某种方式进行了更改,例如,触摸 updated_at,否则不会更新 Active Storage 关系。时间戳,或其他编辑另一个字段 - 我认为这是一个 Rails 错误。

通过添加 <%= f.hidden_field :updated_at, value: DateTime.current %>在我的表单中,我能够强制 Active Storage 保留文件。

希望这对其他人有帮助。

关于ruby-on-rails - 在 Rails 6.0 升级后,ActiveStorage 不会将 blob 持久化到附件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58490421/

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