ruby-on-rails Active Storage在更新时不工作...非常奇怪

oewdyzsn  于 2023-03-24  发布在  Ruby
关注(0)|答案(1)|浏览(168)

流程如下:
我成功地创建了一个 new @文章,没有任何图像表单字段(只有标题文本和描述文本字段)。在@article创建后,我的流程将我重定向到@article编辑页面,在那里我可以提交一个图像(:picture)。此时,当我提交编辑表单(见下文)时,带有:picture图像文件,然后文件被上传,Active Storage工作。

第一次:picture update后,在数据库中得到以下数据:

active_storage_attachments数据库表:

active_storage_blobs数据库表:

active_storage_variant_records数据库表:

在控制台中,我得到如下输出:

Started PATCH "/articles/30" for 127.0.0.1 at 2023-03-20 01:11:59 +0100
Parameters: {"authenticity_token"=>"[FILTERED]", "article"=>{"picture"=>#<ActionDispatch::Http::UploadedFile:0x00007f9d95b4bec0 @tempfile=#<Tempfile:/var/folders/74/bpsjzpcx73vd5601bgrny37w0000gn/T/RackMultipart20230320-14913-yl51y.jpg>, @original_filename="test-image.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"article[picture]\"; filename=\"test-image.jpg\"\r\nContent-Type: image/jpeg\r\n">, "name"=>"Test article", "description"=>"...", ...}, "commit"=>"Update article", "id"=>"30"}
  TRANSACTION (0.1ms)  BEGIN
  ...
  Article Update (0.5ms)  UPDATE `articles` SET ...
  (nothing strange...)
  TRANSACTION (0.8ms)  COMMIT
  ↳ app/controllers/articles_controller.rb:143:in `block in update'
  Disk Storage (1.7ms) Uploaded file to key: bbc964qijs7ymxijt9wt7rwsmk7k (checksum: wJ2jiECv95SxWjqhqdZ7yQ==)
[ActiveJob]   TRANSACTION (0.1ms)  BEGIN
[ActiveJob]   ↳ app/controllers/articles_controller.rb:143:in `block in update'
[ActiveJob]   Delayed::Backend::ActiveRecord::Job Create (0.4ms)  INSERT INTO `delayed_jobs` (`priority`, `attempts`, `handler`, `last_error`, `run_at`, `locked_at`, `failed_at`, `locked_by`, `created_at`, `updated_at`, `queue`) VALUES (0, 0, '--- !ruby/object:ActiveJob::QueueAdapters::DelayedJobAdapter::JobWrapper\njob_data:\n  job_class: ActiveStorage::AnalyzeJob\n  job_id: 036c86a5-a4c7-4966-b5ce-9a2947ac9837\n  provider_job_id:\n  queue_name: default\n  priority:\n  arguments:\n  - _aj_globalid: gid://myproject-app/ActiveStorage::Blob/21\n  executions: 0\n  exception_executions: {}\n  locale: en\n  timezone: Europe/Rome\n  enqueued_at: \'2023-03-20T00:11:59Z\'\n', NULL, '2023-03-20 00:11:59.758725', NULL, NULL, NULL, '2023-03-20 00:11:59.758743', '2023-03-20 00:11:59.758743', 'default')
[ActiveJob]   ↳ app/controllers/articles_controller.rb:143:in `block in update'
[ActiveJob]   TRANSACTION (1.2ms)  COMMIT
[ActiveJob]   ↳ app/controllers/articles_controller.rb:143:in `block in update'
[ActiveJob] Enqueued ActiveStorage::AnalyzeJob (Job ID: 036c86a5-a4c7-4966-b5ce-9a2947ac9837) to DelayedJob(default) with arguments: #<GlobalID:0x00007f9d90b5bdc0 @uri=#<URI::GID gid://myproject-app/ActiveStorage::Blob/21>>

Started GET "/articles/30/edit" for 127.0.0.1 at 2023-03-20 01:11:59 +0100
Processing by ArticlesController#edit as TURBO_STREAM
  Parameters: {"id"=>"30"}
  ...

Started GET "/rails/active_storage/blobs/redirect/eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaHBHZz09IiwiZXhwIjpudWxsLCJwdXIiOiJibG9iX2lkIn19--522f14e56a76e0fbf26782f625205c76f8530780/test-image.jpg" for 127.0.0.1 at 2023-03-20 01:11:59 +0100
Started GET "/rails/active_storage/representations/redirect/eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaHBHZz09IiwiZXhwIjpudWxsLCJwdXIiOiJibG9iX2lkIn19--522f14e56a76e0fbf26782f625205c76f8530780/eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaDdCem9MWm05eWJXRjBTU0lJYW5CbkJqb0dSVlE2Q1dOeWIzQmJDV2tBYVFCcEFjaHBBY2c9IiwiZXhwIjpudWxsLCJwdXIiOiJ2YXJpYXRpb24ifX0=--45bc4f6909e1efc4932a2b96be0e70dd5d0a14ad/test-image.jpg" for 127.0.0.1 at 2023-03-20 01:12:00 +0100
Processing by ActiveStorage::Blobs::RedirectController#show as JPEG
  Parameters: {"signed_id"=>"eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaHBHZz09IiwiZXhwIjpudWxsLCJwdXIiOiJibG9iX2lkIn19--522f14e56a76e0fbf26782f625205c76f8530780", "filename"=>"test-image"}
  ActiveStorage::Blob Load (0.6ms)  SELECT `active_storage_blobs`.* FROM `active_storage_blobs` WHERE `active_storage_blobs`.`id` = 21 LIMIT 1
  Disk Storage (0.3ms) Generated URL for file at key: bbc964qijs7ymxijt9wt7rwsmk7k (http://localhost:3000/rails/active_storage/disk/eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaDdDVG9JYTJWNVNTSWhZbUpqT1RZMGNXbHFjemQ1YlhocGFuUTVkM1EzY25kemJXczNhd1k2QmtWVU9oQmthWE53YjNOcGRHbHZia2tpV1dsdWJHbHVaVHNnWm1sc1pXNWhiV1U5SW5SbGMzUXRhVzFoWjJVdFkzSnZjSEJwYm1jdWFuQm5JanNnWm1sc1pXNWhiV1VxUFZWVVJpMDRKeWQwWlhOMExXbHRZV2RsTFdOeWIzQndhVzVuTG1wd1p3WTdCbFE2RVdOdmJuUmxiblJmZEhsd1pVa2lEMmx0WVdkbEwycHdaV2NHT3daVU9oRnpaWEoyYVdObFgyNWhiV1U2Q214dlkyRnMiLCJleHAiOiIyMDIzLTAzLTIwVDAwOjE3OjAwLjAyM1oiLCJwdXIiOiJibG9iX2tleSJ9fQ==--3af967de95ad5cace03dc6fca438a93020903699/test-image.jpg)
Redirected to http://localhost:3000/rails/active_storage/disk/eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaDdDVG9JYTJWNVNTSWhZbUpqT1RZMGNXbHFjemQ1YlhocGFuUTVkM1EzY25kemJXczNhd1k2QmtWVU9oQmthWE53YjNOcGRHbHZia2tpV1dsdWJHbHVaVHNnWm1sc1pXNWhiV1U5SW5SbGMzUXRhVzFoWjJVdFkzSnZjSEJwYm1jdWFuQm5JanNnWm1sc1pXNWhiV1VxUFZWVVJpMDRKeWQwWlhOMExXbHRZV2RsTFdOeWIzQndhVzVuTG1wd1p3WTdCbFE2RVdOdmJuUmxiblJmZEhsd1pVa2lEMmx0WVdkbEwycHdaV2NHT3daVU9oRnpaWEoyYVdObFgyNWhiV1U2Q214dlkyRnMiLCJleHAiOiIyMDIzLTAzLTIwVDAwOjE3OjAwLjAyM1oiLCJwdXIiOiJibG9iX2tleSJ9fQ==--3af967de95ad5cace03dc6fca438a93020903699/test-image.jpg
Completed 302 Found in 3ms (ActiveRecord: 0.6ms | Allocations: 1450)

Processing by ActiveStorage::Representations::RedirectController#show as JPEG
  Parameters: {"signed_blob_id"=>"eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaHBHZz09IiwiZXhwIjpudWxsLCJwdXIiOiJibG9iX2lkIn19--522f14e56a76e0fbf26782f625205c76f8530780", "variation_key"=>"[FILTERED]", "filename"=>"test-image"}
Started GET "/rails/active_storage/disk/eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaDdDVG9JYTJWNVNTSWhZbUpqT1RZMGNXbHFjemQ1YlhocGFuUTVkM1EzY25kemJXczNhd1k2QmtWVU9oQmthWE53YjNOcGRHbHZia2tpV1dsdWJHbHVaVHNnWm1sc1pXNWhiV1U5SW5SbGMzUXRhVzFoWjJVdFkzSnZjSEJwYm1jdWFuQm5JanNnWm1sc1pXNWhiV1VxUFZWVVJpMDRKeWQwWlhOMExXbHRZV2RsTFdOeWIzQndhVzVuTG1wd1p3WTdCbFE2RVdOdmJuUmxiblJmZEhsd1pVa2lEMmx0WVdkbEwycHdaV2NHT3daVU9oRnpaWEoyYVdObFgyNWhiV1U2Q214dlkyRnMiLCJleHAiOiIyMDIzLTAzLTIwVDAwOjE3OjAwLjAyM1oiLCJwdXIiOiJibG9iX2tleSJ9fQ==--3af967de95ad5cace03dc6fca438a93020903699/test-image.jpg" for 127.0.0.1 at 2023-03-20 01:12:00 +0100
  ActiveStorage::Blob Load (0.6ms)  SELECT `active_storage_blobs`.* FROM `active_storage_blobs` WHERE `active_storage_blobs`.`id` = 21 LIMIT 1
  ActiveStorage::VariantRecord Load (0.2ms)  SELECT `active_storage_variant_records`.* FROM `active_storage_variant_records` WHERE `active_storage_variant_records`.`blob_id` = 21 AND `active_storage_variant_records`.`variation_digest` = 'n0GyErxFaWtygjrlkk9gn7tEL1o=' LIMIT 1
  Disk Storage (0.2ms) Downloaded file from key: bbc964qijs7ymxijt9wt7rwsmk7k
  TRANSACTION (0.3ms)  BEGIN
  ActiveStorage::VariantRecord Create (0.5ms)  INSERT INTO `active_storage_variant_records` (`blob_id`, `variation_digest`) VALUES (21, 'n0GyErxFaWtygjrlkk9gn7tEL1o=')
  ActiveStorage::Blob Load (0.5ms)  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` = 11 AND `active_storage_attachments`.`record_type` = 'ActiveStorage::VariantRecord' AND `active_storage_attachments`.`name` = 'image' LIMIT 1
  ActiveStorage::Attachment Load (0.4ms)  SELECT `active_storage_attachments`.* FROM `active_storage_attachments` WHERE `active_storage_attachments`.`record_id` = 11 AND `active_storage_attachments`.`record_type` = 'ActiveStorage::VariantRecord' AND `active_storage_attachments`.`name` = 'image' LIMIT 1
  ActiveStorage::Blob Create (0.3ms)  INSERT INTO `active_storage_blobs` (`key`, `filename`, `content_type`, `metadata`, `service_name`, `byte_size`, `checksum`, `created_at`) VALUES ('3fw2dy26zqhafoxpx50pot4d4fzi', 'test-image.jpg', 'image/jpeg', '{\"identified\":true}', 'local', 12939, 'ZCyRwPda7dlCsen1K5AsUA==', '2023-03-20 00:12:00.123066')
  ActiveStorage::Attachment Create (0.4ms)  INSERT INTO `active_storage_attachments` (`name`, `record_type`, `record_id`, `blob_id`, `created_at`) VALUES ('image', 'ActiveStorage::VariantRecord', 11, 22, '2023-03-20 00:12:00.124641')
  TRANSACTION (1.2ms)  COMMIT
  Disk Storage (1.3ms) Uploaded file to key: 3fw2dy26zqhafoxpx50pot4d4fzi (checksum: ZCyRwPda7dlCsen1K5AsUA==)
[ActiveJob]   TRANSACTION (0.2ms)  BEGIN
[ActiveJob]   Delayed::Backend::ActiveRecord::Job Create (0.5ms)  INSERT INTO `delayed_jobs` (`priority`, `attempts`, `handler`, `last_error`, `run_at`, `locked_at`, `failed_at`, `locked_by`, `created_at`, `updated_at`, `queue`) VALUES (0, 0, '--- !ruby/object:ActiveJob::QueueAdapters::DelayedJobAdapter::JobWrapper\njob_data:\n  job_class: ActiveStorage::AnalyzeJob\n  job_id: 214d74b2-189f-4199-9229-6033de71f173\n  provider_job_id:\n  queue_name: default\n  priority:\n  arguments:\n  - _aj_globalid: gid://myproject-app/ActiveStorage::Blob/22\n  executions: 0\n  exception_executions: {}\n  locale: en\n  timezone: Europe/Rome\n  enqueued_at: \'2023-03-20T00:12:00Z\'\n', NULL, '2023-03-20 00:12:00.130936', NULL, NULL, NULL, '2023-03-20 00:12:00.130957', '2023-03-20 00:12:00.130957', 'default')
[ActiveJob]   TRANSACTION (0.4ms)  COMMIT
[ActiveJob] Enqueued ActiveStorage::AnalyzeJob (Job ID: 214d74b2-189f-4199-9229-6033de71f173) to DelayedJob(default) with arguments: #<GlobalID:0x00007f9d90d49e98 @uri=#<URI::GID gid://myproject-app/ActiveStorage::Blob/22>>
  Disk Storage (0.3ms) Generated URL for file at key: 3fw2dy26zqhafoxpx50pot4d4fzi (http://localhost:3000/rails/active_storage/disk/eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaDdDVG9JYTJWNVNTSWhNMlozTW1SNU1qWjZjV2hoWm05NGNIZzFNSEJ2ZERSa05HWjZhUVk2QmtWVU9oQmthWE53YjNOcGRHbHZia2tpV1dsdWJHbHVaVHNnWm1sc1pXNWhiV1U5SW5SbGMzUXRhVzFoWjJVdFkzSnZjSEJwYm1jdWFuQm5JanNnWm1sc1pXNWhiV1VxUFZWVVJpMDRKeWQwWlhOMExXbHRZV2RsTFdOeWIzQndhVzVuTG1wd1p3WTdCbFE2RVdOdmJuUmxiblJmZEhsd1pVa2lEMmx0WVdkbEwycHdaV2NHT3daVU9oRnpaWEoyYVdObFgyNWhiV1U2Q214dlkyRnMiLCJleHAiOiIyMDIzLTAzLTIwVDAwOjE3OjAwLjEzNVoiLCJwdXIiOiJibG9iX2tleSJ9fQ==--4a9dd02fb53f7530fd79757bee3ff3d07774428b/test-image.jpg)
Redirected to http://localhost:3000/rails/active_storage/disk/eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaDdDVG9JYTJWNVNTSWhNMlozTW1SNU1qWjZjV2hoWm05NGNIZzFNSEJ2ZERSa05HWjZhUVk2QmtWVU9oQmthWE53YjNOcGRHbHZia2tpV1dsdWJHbHVaVHNnWm1sc1pXNWhiV1U5SW5SbGMzUXRhVzFoWjJVdFkzSnZjSEJwYm1jdWFuQm5JanNnWm1sc1pXNWhiV1VxUFZWVVJpMDRKeWQwWlhOMExXbHRZV2RsTFdOeWIzQndhVzVuTG1wd1p3WTdCbFE2RVdOdmJuUmxiblJmZEhsd1pVa2lEMmx0WVdkbEwycHdaV2NHT3daVU9oRnpaWEoyYVdObFgyNWhiV1U2Q214dlkyRnMiLCJleHAiOiIyMDIzLTAzLTIwVDAwOjE3OjAwLjEzNVoiLCJwdXIiOiJibG9iX2tleSJ9fQ==--4a9dd02fb53f7530fd79757bee3ff3d07774428b/test-image.jpg
Completed 302 Found in 59ms (ActiveRecord: 5.5ms | Allocations: 23307)

Processing by ActiveStorage::DiskController#show as JPEG
  Parameters: {"encoded_key"=>"[FILTERED]", "filename"=>"test-image"}
Completed 200 OK in 1ms (ActiveRecord: 0.0ms | Allocations: 315)

Started GET "/rails/active_storage/disk/eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaDdDVG9JYTJWNVNTSWhNMlozTW1SNU1qWjZjV2hoWm05NGNIZzFNSEJ2ZERSa05HWjZhUVk2QmtWVU9oQmthWE53YjNOcGRHbHZia2tpV1dsdWJHbHVaVHNnWm1sc1pXNWhiV1U5SW5SbGMzUXRhVzFoWjJVdFkzSnZjSEJwYm1jdWFuQm5JanNnWm1sc1pXNWhiV1VxUFZWVVJpMDRKeWQwWlhOMExXbHRZV2RsTFdOeWIzQndhVzVuTG1wd1p3WTdCbFE2RVdOdmJuUmxiblJmZEhsd1pVa2lEMmx0WVdkbEwycHdaV2NHT3daVU9oRnpaWEoyYVdObFgyNWhiV1U2Q214dlkyRnMiLCJleHAiOiIyMDIzLTAzLTIwVDAwOjE3OjAwLjEzNVoiLCJwdXIiOiJibG9iX2tleSJ9fQ==--4a9dd02fb53f7530fd79757bee3ff3d07774428b/test-image.jpg" for 127.0.0.1 at 2023-03-20 01:12:00 +0100
Processing by ActiveStorage::DiskController#show as JPEG
  Parameters: {"encoded_key"=>"[FILTERED]", "filename"=>"test-image"}
Completed 200 OK in 1ms (ActiveRecord: 0.0ms | Allocations: 222)

从这次开始(第一次:picture更新后),Active Storage似乎不再工作了,即当我尝试更新@article图像时,图像不再上传。我可以成功删除图像(使用下面的:delete_picture),但当我尝试提交新图像时,它不再上传。

删除图片后,我在数据库中有以下数据:

active_storage_attachments数据库表:

active_storage_blobs数据库表:

active_storage_variant_records数据库表:

在控制台中,我得到了指示图片被成功删除的输出,即TRANSACTION开始,DELETE FROM active_storage_attachmentsactive_storage_blobs,COMMIT。

删除:picture后,我尝试提交一个新的:picture图片,然后在控制台得到如下输出:

Started PATCH "/articles/30" for 127.0.0.1 at 2023-03-20 01:31:00 +0100
Processing by ArticlesController#update as TURBO_STREAM
  Parameters: {"authenticity_token"=>"[FILTERED]", "article"=>{"picture"=>#<ActionDispatch::Http::UploadedFile:0x00007f9d94a4e730 @tempfile=...
  ...
  TRANSACTION (0.1ms)  BEGIN
  ...
  TRANSACTION (0.2ms)  COMMIT
Completed 302 Found in 51ms (ActiveRecord: 16.5ms | Allocations: 27855)

Started GET "/articles/30/edit" for 127.0.0.1 at 2023-03-20 01:31:00 +0100
Processing by ArticlesController#edit as TURBO_STREAM
  ...
Completed 200 OK in 85ms (Views: 53.6ms | ActiveRecord: 22.8ms | Allocations: 70410)

未提及活动存储和活动作业。
在我的模型文章.rb文件中,我有:

class Article < ApplicationRecord
  has_one_attached :picture

  attr_accessor :delete_picture

  after_save :trigger_delete_picture, :if => :delete_picture?
  
  ...

  private

  def trigger_delete_picture
    self.picture.purge
  end

  def delete_picture?
    self.delete_picture == '1' ? true : false
  end
end

在我的控制器articles_controller.rb文件中,我有:

class ArticlesController < ApplicationController
  def update
    @article = Article.find_by(:id => params[:id])

    respond_to do |format|
      if @article.update(article_params)
        format.html { redirect_to(edit_article_path(@article)) }
      else
        format.html { render(:action => :edit, :status => 422) }
      end
    end
  end

  ...
  
  def article_params
    params.require(:article).permit(:picture, :delete_picture, ...)
  end
end

在我的视图edit.rb文件中,我有:

<%= bootstrap_form_for(@article) do |form| %>

  <%= form.file_field(:picture) %>

  <% if @article.picture.attached? %>
    <%= form.check_box :delete_picture %>

    <%= image_tag(@article.picture.variant(:crop => [0, 0, 200, 200])) %>
  <% end %>
  ...
<% end %>

我正在使用Ruby on Rails7.0.4.2和Turbo,并在Ruby 2.7.2p137中安装了所有默认和必需的gem(gem“image_processing”,“~〉1.2”,gem“ruby-vips”等)。
有什么问题吗?

z8dt9xmd

z8dt9xmd1#

对不起,我的错。我有一个模型示例在流程中重新加载,这导致了我的问题!!!

相关问题