PR *_*ead 4 ruby ruby-on-rails rails-activestorage ruby-on-rails-6
我有一个 ActiveStorage 问题,似乎是由于 Rails 6.0 升级(从 5.2.x)引起的。
我有一个自定义管理界面,其中页面有字段,每个字段都有一个 STItype来创建复杂的页面布局。该Image类型使用Active存储(这是回形针,但出货的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
Run Code Online (Sandbox Code Playgroud)
当页面更新时,它通过嵌套的表单属性和一个简单的调用@page.update(permitted_params).
该permitted_params(减去不重要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"
}
}
}
Run Code Online (Sandbox Code Playgroud)
为了维护更新集合的 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
Run Code Online (Sandbox Code Playgroud)
所以这让我认为这可能与嵌套参数有关,但我看不到这些问题,所以我对可能发生的事情感到困惑。
图片上传在网站的其他地方正常运行,只是在这个地方它们没有按预期工作。
任何帮助将不胜感激。
谢谢你,保罗。
编辑:
我已经将范围缩小到 ActiveStorage 不更新图像关联,除非模型上的字段(updated_at或表中的任何列)也更新。这似乎会触发 ActiveRecord 来持久化实例,从而触发 ActiveStorage 上传文件。
经过一番尝试后,我发现除非模型以某种方式进行了更改,例如,触摸updated_at时间戳或其他编辑另一个字段,否则不会更新 Active Storage 关系- 我认为这是一个 Rails 错误。
通过<%= f.hidden_field :updated_at, value: DateTime.current %>在我的表单中添加,我能够强制 Active Storage 保留文件。
希望这对其他人有帮助。
| 归档时间: |
|
| 查看次数: |
3313 次 |
| 最近记录: |