Rails 6, Paperclip, S3, s3_direct_upload @attachment.save s3_direct_upload 完成后不保存

Rails 6, Paperclip, S3, s3_direct_upload @attachment.save not saving after the s3_direct_upload completes

我已经从 Rails v4.2 的旧版本到 Rails v6 重写了一个名为 FilterTrak 的应用程序,除了图片上传的一部分,我现在一切正常。

该应用程序使用 Paperclip、aws-sdk-v1 和 s3_direct_upload。我之前遇到了 s3_direct_upload 的问题,我开始工作了。我现在遇到的问题是,在将文件上传到 s3 之后,它应该保存附件,这会给我一个来自数据库的 ID,然后调用应该启动的辅助进程。

由于未知原因,@attachment.save 没有保存,因为它没有保存,所以没有回调到第二个进程,该进程应该将文件移动到 S3 上的 perm 位置,然后保存该信息进入数据库。我曾尝试手动调用第二个进程,但由于附件未保存,因此未生成 ID。由于我没有可使用的 ID,因此我无法手动或以其他方式调用第二个流程。我相信我已经解决了控制器和 @attachment.save 行的问题。如果我在那之后检查@attachment.save,我会得到@attachment.save FALSE。此代码目前在 Rails 的旧版本上正常工作。所以我假设由于新版本 Rails 中的某些更改,这部分无法正常工作。我花了 2 个多星期的时间试图解决这个问题,但我没有取得任何进展。我正在考虑更改应用程序以使用活动存储,但由于 @attachment.save 是我认为不起作用的部分,我不知道这是否真的有任何好处,因为文件上传似乎没有据我所知是问题所在。因为它确实成功上传到我的 S3 存储桶到 /uploads 文件夹中。

这是 attachments_controller.rb 文件。

class AttachmentsController < ApplicationController
  load_and_authorize_resource except: [:create]
  skip_authorization_check only: [:create]

  before_action :set_service, only: [:create, :update, :destroy]
  before_action :set_attachment, only: [:update, :destroy]

  def create!
    @attachment = @service.attachments.new(attachment_params)
    @attachment.when = params[:when]
    @attachment.save 
  end

  def update
    respond_to do |format|
      if @attachment.update(attachment_params)
        format.html { redirect_to @attachment, notice: 'Attachment was successfully updated.' }
        format.json { render :show, status: :ok, location: @attachment }
      else
        format.html { render :edit }
        format.json { render json: @attachment.errors, status: :unprocessable_entity }
      end
    end
  end

  def destroy
    @attachment.destroy
    respond_to do |format|
      format.html { redirect_to customer_filter_service_path(@customer, @filter, @service), notice: 'Picture was successfully removed.' }
      format.json { head :no_content }
    end
  end

  private
    def set_service
      @customer = Customer.find(params[:customer_id])
      @filter = @customer.filters.find(params[:filter_id])
      @service = @filter.services.find(params[:service_id])
      #@attachment = Attachment.find(params[:id])
    end

    def set_attachment
      @attachment = Attachment.find(params[:id])
    end

    def attachment_params
      params.require(:attachment).permit(:direct_upload_url)
    end
end

这里是 attachments.rb 模型(将桶名称更改为 mybucket)

# == Schema Information
#
# Table name: attachments
#
#  id                  :integer          not null, primary key
#  service_id          :integer
#  user_id             :integer
#  direct_upload_url   :string           not null
#  upload_file_name    :string
#  upload_content_type :string
#  upload_file_size    :integer
#  upload_updated_at   :datetime
#  processed           :boolean          default("false"), not null
#  created_at          :datetime         not null
#  updated_at          :datetime         not null
#  when                :string
#

class Attachment < ApplicationRecord
  # Environment-specific direct upload url verifier screens for malicious posted upload locations.

  DIRECT_UPLOAD_URL_FORMAT = %r{\Ahttps:\/\/mybucket#{!Rails.env.production? ? "\-#{Rails.env}" : ''}\.s3\.amazonaws\.com\/(?<path>uploads\/.+\/(?<filename>.+))\z}.freeze

  #attr_accessor :id, :service_id, :user_id, :direct_upload_url, :upload_file_name, 
  #:upload_content_type, :upload_file_size, :upload_updated_at, :processed, :created_at, :updated_at, :when       

  belongs_to :service
  belongs_to :user  
  has_attached_file :upload,
    path: 'service/:filter_id/attachments/:id-:style-:filename',
    styles: { medium: "200x200" }
 
  validates :direct_upload_url, presence: true, format: { with: DIRECT_UPLOAD_URL_FORMAT }
  validates_attachment_content_type :upload, :content_type => ["image/jpg", "image/jpeg", "image/png"]
  validates_presence_of :when

  before_create :set_upload_attributes
  after_create :queue_processing
    
  # interpolate in paperclip
  Paperclip.interpolates :filter_id  do |p, _|
    p.instance.service.filter_id
  end

  # Store an unescaped version of the escaped URL that Amazon returns from direct upload.
  def direct_upload_url=(escaped_url)
    write_attribute(:direct_upload_url, (CGI.unescape(escaped_url) rescue nil))
  end
    
  # Determines if file requires post-processing (image resizing, etc)
  def post_process_required?
    %r{^(image|(x-)?application)/(jpeg|jpg|pjpeg|png|x-png)$}.match(upload_content_type).present?
  end
  
  # Final upload processing step
  def self.transfer_and_cleanup(id)
    attachment = Attachment.find(id)
    direct_upload_url_data = DIRECT_UPLOAD_URL_FORMAT.match(attachment.direct_upload_url)

    s3 = AWS::S3.new
    
    if attachment.post_process_required?
      # this has an issue removing brackets
      attachment.upload = URI.parse(URI.escape(attachment.direct_upload_url))
    else
      paperclip_file_path = "service/filter-#{filter_id}/service-#{service_id}/photos/#{direct_upload_url_data[:filename]}"
      s3.buckets[Rails.configuration.aws[:bucket]].objects[paperclip_file_path].copy_from(direct_upload_url_data[:path])
    end
 
    attachment.processed = true
    attachment.save
    
    s3.buckets[Rails.configuration.aws[:bucket]].objects[direct_upload_url_data[:path]].delete
  end

  def before_install?
    self.when == "before"
  end

  def after_install?
    self.when == "after"
  end

  protected
  
  # Set attachment attributes from the direct upload
  # @note Retry logic handles S3 "eventual consistency" lag.
  def set_upload_attributes
    tries ||= 5
    direct_upload_url_data = DIRECT_UPLOAD_URL_FORMAT.match(direct_upload_url)
    s3 = AWS::S3.new
    direct_upload_head = s3.buckets[Rails.configuration.aws[:bucket]].objects[direct_upload_url_data[:path]].head
 
    self.upload_file_name     = direct_upload_url_data[:filename]
    self.upload_file_size     = direct_upload_head.content_length
    self.upload_content_type  = direct_upload_head.content_type
    self.upload_updated_at    = direct_upload_head.last_modified
  rescue AWS::S3::Errors::NoSuchKey => e
    tries -= 1
    if tries > 0
      sleep(3)
      retry
    else
      false
    end
  end
  
  # Queue file processing
  def queue_processing
    # Attachment.delay.transfer_and_cleanup(id)
    Attachment.transfer_and_cleanup(id)

    # bust service cache
    service.touch
  end

  scope :before_install, -> { where( when: "before" ) }
  scope :after_install, -> { where( when: "after" ) }
end

这里是视图文件的相关部分:

%br.visible-print/
    %br.visible-print/
    .panel.panel-default.page-break
      .panel-heading
        .row
          .col-md-6
            .pull-left
              Pictures
      .panel-body
        .row
          .col-md-12
            .row
              .col-md-12
                %h4.strong Before Cleaning Photos
            .row.hidden-print
              .col-md-12
                .well#before-pictures-dropzone.dropzone
                  %h4 Click to Add
                  = s3_uploader_form id: "attachment_before",
                    callback_url: customer_filter_service_attachments_url(@customer, @filter, @service, when: "before"),
                    callback_param: "attachment[direct_upload_url]",
                    key_starts_with: "uploads/",
                    key: "uploads/{timestamp}-{unique_id}-#{SecureRandom.hex}/${filename}",
                    max_file_size: 100.megabytes do
                    = file_field_tag(:file, class: "attachment", multiple: true, data: { url: s3_uploader_url })
                  #before_uploads_container
                  %script#template-upload{:type => "text/x-tmpl"}
                    <div id="upload_{%=o.unique_id%}" class="upload">
                    <h5>{%=o.name%}</h5>
                    <div class="progress progress-striped active"><div class="bar" style="width: 0%"></div></div>
                    </div>
            .row
              .col-md-12
                - if @before_photos
                  = render @before_photos, gallery: "before"
                - else
                  %h5 No Photos Added Yet
                #before_photos_container

这是路由文件中处理该部分的部分:

resources :customers do
    resources :filters, except: [:index, :show] do
      resources :services, except: [:index, :show] do
        post 'email', on: :member
        resources :attachments, except: [:index, :show]
      end
    end
  end

这是上传过程的开始,在您点击“选择文件”按钮和 select 您的一个或多个文件后,再次天气它是 1 个文件或 10 个文件,它们都成功上传到 S3 到 /上传文件夹。进度条出现、构建、完成,然后像预期的那样消失。第二个进程永远不会启动。

Started POST "/customers/419/filters/990/services/1137/attachments?when=before" for ::1 at 2021-11-10 09:07:42 -0800
Processing by AttachmentsController#create as */*
  Parameters: {"url"=>"https://mybucket.s3.amazonaws.com/uploads%2F1636564061858-fi8lg02378-65280638d6d13515e74449339d1aa926%2FDES+LOGO+%28512x512%29.jpg", "filepath"=>"/uploads%2F1636564061858-fi8lg02378-65280638d6d13515e74449339d1aa926%2FDES+LOGO+%28512x512%29.jpg", "filename"=>"DES LOGO (512x512).jpg", "filesize"=>"86383", "lastModifiedDate"=>"Wed May 24 2017 11:55:08 GMT-0700 (Pacific Daylight Time)", "filetype"=>"image/jpeg", "unique_id"=>"fi8lg02378", "attachment"=>{"direct_upload_url"=>"https://mybucket.s3.amazonaws.com/uploads%2F1636564061858-fi8lg02378-65280638d6d13515e74449339d1aa926%2FDES+LOGO+%28512x512%29.jpg"}, "when"=>"before", "customer_id"=>"419", "filter_id"=>"990", "service_id"=>"1137"}
C:/Ruby27-x64/lib/ruby/gems/2.7.0/gems/json-1.8.6/lib/json/common.rb:155: warning: Using the last argument as keyword parameters is deprecated
C:/Ruby27-x64/lib/ruby/gems/2.7.0/gems/json-1.8.6/lib/json/common.rb:155: warning: Using the last argument as keyword parameters is deprecated
  User Load (0.9ms)  SELECT "users".* FROM "users" WHERE "users"."id" =  ORDER BY "users"."id" ASC LIMIT   [["id", 34], ["LIMIT", 1]]
  Customer Load (1.0ms)  SELECT "customers".* FROM "customers" WHERE "customers"."id" =  LIMIT  /*line:/app/controllers/attachments_controller.rb:42:in `set_service'*/  [["id", 419], ["LIMIT", 1]]
  ↳ app/controllers/attachments_controller.rb:42:in `set_service'
  Filter Load (0.9ms)  SELECT "filters".* FROM "filters" WHERE "filters"."customer_id" =  AND "filters"."id" =  LIMIT  /*line:/app/controllers/attachments_controller.rb:43:in `set_service'*/  [["customer_id", 419], ["id", 990], ["LIMIT", 1]]
  ↳ app/controllers/attachments_controller.rb:43:in `set_service'
  Service Load (1.0ms)  SELECT "services".* FROM "services" WHERE "services"."filter_id" =  AND "services"."id" =  LIMIT  /*line:/app/controllers/attachments_controller.rb:44:in `set_service'*/  [["filter_id", 990], ["id", 1137], ["LIMIT", 1]]
  ↳ app/controllers/attachments_controller.rb:44:in `set_service'
Attachment Load (0.9ms)  SELECT "attachments".* FROM "attachments" WHERE "attachments"."service_id" =  /*line:/app/controllers/attachments_controller.rb:11:in `create'*/  [["service_id", 1137]]
  ↳ app/controllers/attachments_controller.rb:11:in `create'

:: RIGHT HERE IS WHERE A SECOND PROCCESS IS SUPPOSED TO START ::

  Rendering attachments/create.js.erb
  Rendered attachments/create.js.erb (Duration: 0.1ms | Allocations: 8)
Completed 200 OK in 29ms (Views: 1.6ms | ActiveRecord: 4.8ms | Allocations: 10698)

这是第二个进程应该是什么,这是从当前 运行 在 Heroku 上的应用程序中删除的。

2021-10-25T23:13:27.756616+00:00 app[web.1]:  (0.5ms)  BEGIN
2021-10-25T23:13:27.802287+00:00 app[web.1]: [AWS S3 200 0.044684 0 retries] head_object(:bucket_name=>"dpfregen",:key=>"uploads/1635203641183-o14kckjjp6d-1311ed2cf3237c252d35885ba8bbd47a/DES LOGO (512x512).jpg")
2021-10-25T23:13:27.802289+00:00 app[web.1]:
2021-10-25T23:13:27.805869+00:00 app[web.1]: SQL (2.3ms)  INSERT INTO "attachments" ("direct_upload_url", "service_id", "when", "created_at", "updated_at", "upload_file_name", "upload_file_size", "upload_content_type", "upload_updated_at") VALUES (, , , , , , , , ) RETURNING "id"  [["direct_upload_url", "https://dpfregen.s3.amazonaws.com/uploads/1635203641183-o14kckjjp6d-1311ed2cf3237c252d35885ba8bbd47a/DES LOGO (512x512).jpg"], ["service_id", 1137], ["when", "before"], ["created_at", "2021-10-25 23:13:27.756915"], ["updated_at", "2021-10-25 23:13:27.756915"], ["upload_file_name", "DES LOGO (512x512).jpg"], ["upload_file_size", 86383], ["upload_content_type", "image/jpeg"], ["upload_updated_at", "2021-10-25 23:13:28.000000"]]
2021-10-25T23:13:27.807045+00:00 app[web.1]: Attachment Load (0.7ms)  SELECT  "attachments".* FROM "attachments" WHERE "attachments"."id" =  LIMIT 1  [["id", 106905]]
2021-10-25T23:13:27.834504+00:00 app[web.1]: Service Load (0.9ms)  SELECT  "services".* FROM "services" WHERE "services"."id" =  LIMIT 1  [["id", 1137]]
2021-10-25T23:13:27.852128+00:00 app[web.1]: [AWS S3 404 0.016364 0 retries] head_object(:bucket_name=>"dpfregen",:key=>"service/990/attachments/106905-original-DES LOGO (512x512).jpg") AWS::S3::Errors::NoSuchKey No Such Key
2021-10-25T23:13:27.852129+00:00 app[web.1]:
2021-10-25T23:13:27.869707+00:00 app[web.1]: [AWS S3 404 0.017152 0 retries] head_object(:bucket_name=>"dpfregen",:key=>"service/990/attachments/106905-medium-DES LOGO (512x512).jpg") AWS::S3::Errors::NoSuchKey No Such Key
2021-10-25T23:13:27.869708+00:00 app[web.1]:
2021-10-25T23:13:27.871399+00:00 app[web.1]: Command :: file -b --mime '/tmp/019c51a707e3a0c3ea3263003bcd49ce20211025-6-1mcjn6k.jpg'
2021-10-25T23:13:27.874638+00:00 app[web.1]: Command :: identify -format '%wx%h,%[exif:orientation]' '/tmp/97bb878daac24e395c0721c1be8733cd20211025-6-1l11e2y.jpg[0]' 2>/dev/null
2021-10-25T23:13:27.891231+00:00 app[web.1]: Command :: identify -format %m '/tmp/97bb878daac24e395c0721c1be8733cd20211025-6-1l11e2y.jpg[0]'
2021-10-25T23:13:27.896884+00:00 app[web.1]: Command :: convert '/tmp/97bb878daac24e395c0721c1be8733cd20211025-6-1l11e2y.jpg[0]' -auto-orient -resize "200x200" '/tmp/4cc67e7e3d2e785af805f6ffc9cc0e8220211025-6-17zwb7y'
2021-10-25T23:13:27.945653+00:00 app[web.1]: Command :: file -b --mime '/tmp/019c51a707e3a0c3ea3263003bcd49ce20211025-6-w4tdp8.jpg'
2021-10-25T23:13:27.951181+00:00 app[web.1]: SQL (0.8ms)  UPDATE "attachments" SET "upload_file_name" = , "upload_updated_at" = , "processed" = , "updated_at" =  WHERE "attachments"."id" =   [["upload_file_name", "DES_20LOGO_20(512x512).jpg"], ["upload_updated_at", "2021-10-25 23:13:27.870718"], ["processed", "t"], ["updated_at", "2021-10-25 23:13:27.948459"], ["id", 106905]]
2021-10-25T23:13:27.951557+00:00 app[web.1]: [paperclip] saving service/990/attachments/106905-original-DES_20LOGO_20(512x512).jpg
2021-10-25T23:13:27.986837+00:00 app[web.1]: [AWS S3 200 0.034825 0 retries] put_object(:acl=>:private,:bucket_name=>"dpfregen",:content_length=>86383,:content_type=>"image/jpeg",:data=>Paperclip::UriAdapter: DES%20LOGO%20(512x512).jpg,:key=>"service/990/attachments/106905-original-DES_20LOGO_20(512x512).jpg")
2021-10-25T23:13:27.986839+00:00 app[web.1]:
2021-10-25T23:13:27.986964+00:00 app[web.1]: [paperclip] saving service/990/attachments/106905-medium-DES_20LOGO_20(512x512).jpg
2021-10-25T23:13:28.017915+00:00 app[web.1]: [AWS S3 200 0.029985 0 retries] put_object(:acl=>:private,:bucket_name=>"dpfregen",:content_length=>40823,:content_type=>"image/jpeg",:data=>Paperclip::FileAdapter: 4cc67e7e3d2e785af805f6ffc9cc0e8220211025-6-17zwb7y,:key=>"service/990/attachments/106905-medium-DES_20LOGO_20(512x512).jpg")
2021-10-25T23:13:28.017923+00:00 app[web.1]:
2021-10-25T23:13:28.044967+00:00 app[web.1]: [AWS S3 204 0.026224 0 retries] delete_object(:bucket_name=>"dpfregen",:key=>"uploads/1635203641183-o14kckjjp6d-1311ed2cf3237c252d35885ba8bbd47a/DES LOGO (512x512).jpg")
2021-10-25T23:13:28.044969+00:00 app[web.1]:
2021-10-25T23:13:28.047863+00:00 app[web.1]: SQL (0.8ms)  UPDATE "services" SET "updated_at" = '2021-10-25 23:13:28.045149' WHERE "services"."id" =   [["id", 1137]]
2021-10-25T23:13:28.050243+00:00 app[web.1]: SQL (0.6ms)  UPDATE "filters" SET "updated_at" = '2021-10-25 23:13:28.048146' WHERE "filters"."id" =   [["id", 990]]
2021-10-25T23:13:28.051630+00:00 app[web.1]: Company Load (0.6ms)  SELECT  "companies".* FROM "companies" WHERE "companies"."id" =  LIMIT 1  [["id", 1]]
2021-10-25T23:13:28.054209+00:00 app[web.1]: SQL (0.6ms)  UPDATE "companies" SET "updated_at" = '2021-10-25 23:13:28.052156' WHERE "companies"."id" =   [["id", 1]]
2021-10-25T23:13:28.056432+00:00 app[web.1]:  (1.9ms)  COMMIT

:: THEN IT GOES TO THE ENDING SECTION JUST LIKE MY CURRENT APP IS DOING BUT MY CURRENT APP IS SKIPPING ALL OF THE ABOVE ::

2021-10-25T23:13:28.060278+00:00 app[web.1]: Rendered attachments/_attachment.html.haml (2.6ms)
2021-10-25T23:13:28.060371+00:00 app[web.1]: Rendered attachments/create.js.erb (3.0ms)
2021-10-25T23:13:28.060540+00:00 app[web.1]: Completed 200 OK in 312ms (Views: 3.8ms | ActiveRecord: 12.1ms)

这是@service.attachments

的转储
@service.attachments: #<ActiveRecord::Associations::CollectionProxy [#<Attachment id: 3391, service_id: 1137, user_id: nil, direct_upload_url: "https://mybucket.s3.amazonaws.com/uploads/14672362...", upload_file_name: "20160629_141838.jpg", upload_content_type: "image/jpeg", upload_file_size: 664048, upload_updated_at: "2016-06-29 14:37:55.771560000 -0700", processed: true, created_at: "2016-06-29 14:37:55.511793000 -0700", updated_at: "2016-06-29 14:37:55.948425000 -0700", when: "before">, #<Attachment id: 3389, service_id: 1137, user_id: nil, direct_upload_url: "https://mybucket.s3.amazonaws.com/uploads/14672362...", upload_file_name: "20160629_141808.jpg", upload_content_type: "image/jpeg", upload_file_size: 624308, upload_updated_at: "2016-06-29 14:37:42.278770000 -0700", processed: true, created_at: "2016-06-29 14:37:42.134005000 -0700", updated_at: "2016-06-29 14:37:42.484505000 -0700", when: "before">, #<Attachment id: 3390, service_id: 1137, user_id: nil, direct_upload_url: "https://mybucket.s3.amazonaws.com/uploads/14672362...", upload_file_name: "20160629_141758.jpg", upload_content_type: "image/jpeg", upload_file_size: 675360, upload_updated_at: "2016-06-29 14:37:53.449659000 -0700", processed: true, created_at: "2016-06-29 14:37:53.068395000 -0700", updated_at: "2016-06-29 14:37:53.671685000 -0700", when: "before">, #<Attachment id: 3437, service_id: 1137, user_id: nil, direct_upload_url: "https://mybucket.s3.amazonaws.com/uploads/14673121...", upload_file_name: "1467312187074-1193375171.jpg", upload_content_type: "image/jpeg", upload_file_size: 639235, upload_updated_at: "2016-06-30 11:42:58.287106000 -0700", processed: true, created_at: "2016-06-30 11:42:57.996008000 -0700", updated_at: "2016-06-30 11:42:59.051767000 -0700", when: "after">, #<Attachment id: 3439, service_id: 1137, user_id: nil, direct_upload_url: "https://mybucket.s3.amazonaws.com/uploads/14673122...", upload_file_name: "1467312251285-1977276610.jpg", upload_content_type: "image/jpeg", upload_file_size: 877201, upload_updated_at: "2016-06-30 11:44:01.036497000 -0700", processed: true, created_at: "2016-06-30 11:44:00.642325000 -0700", updated_at: "2016-06-30 11:44:01.653891000 -0700", when: "after">, 
:: THIS BELOW IS THE FILE THAT WAS JUST UPLOADED, NOTICE THE attachment_id is NILL, this is because it hasn't been saved ::
#<Attachment id: nil, service_id: 1137, user_id: nil, direct_upload_url: "https://mybucket.s3.amazonaws.com/uploads/16365640...", upload_file_name: nil, upload_content_type: nil, upload_file_size: nil, upload_updated_at: nil, processed: false, created_at: nil, updated_at: nil, when: nil>]>

这是同一时间点的@附件:

@attachment: #<Attachment id: nil, service_id: 1137, user_id: nil, direct_upload_url: "https://mybucket.s3.amazonaws.com/uploads/16365640...", upload_file_name: nil, upload_content_type: nil, upload_file_size: nil, upload_updated_at: nil, processed: false, created_at: nil, updated_at: nil, when: nil>

这是同时转储的参数:

params#<ActionController::Parameters {"url"=>"https://mybucket.s3.amazonaws.com/uploads%2F1636564061858-fi8lg02378-65280638d6d13515e74449339d1aa926%2FDES+LOGO+%28512x512%29.jpg", "filepath"=>"/uploads%2F1636564061858-fi8lg02378-65280638d6d13515e74449339d1aa926%2FDES+LOGO+%28512x512%29.jpg", "filename"=>"DES LOGO (512x512).jpg", "filesize"=>"86383", "lastModifiedDate"=>"Wed May 24 2017 11:55:08 GMT-0700 (Pacific Daylight Time)", "filetype"=>"image/jpeg", "unique_id"=>"fi8lg02378", "attachment"=>#<ActionController::Parameters {"direct_upload_url"=>"https://mybucket.s3.amazonaws.com/uploads%2F1636564061858-fi8lg02378-65280638d6d13515e74449339d1aa926%2FDES+LOGO+%28512x512%29.jpg"} permitted: true>, "when"=>"before", "controller"=>"attachments", "action"=>"create", "customer_id"=>"419", "filter_id"=>"990", "service_id"=>"1137"} permitted: true>

如果我检查@attachment.save,我得到这个:

@attachment.save:false

据我所知,在 after_create 回调中调用 :queue_processing 的第二个进程未启动的原因是 @attachement.save 未保存。为什么它不节省我不明白。我已经尝试了各种手动方法来尝试强制保存但它不起作用,我尝试添加@attachment = Attachment.save,我尝试添加@attachment = Attachment.create(attachment_params ), 我尝试在 @attachment.save 之前添加 @attachment = Attachment.create(attachment_params[:id]) 并且无论我做什么它都不会保存,我只是不明白为什么它不会保存。旧版本 rails 上的相同代码确实保存并在那时工作。

如果有人有任何想法,我很想听听,将不胜感激。我假设问题来自 @attachment.save 但我想也可能是 s3_direct_upload 没有正确进行回调,但我不知道如何解决该部分。我在 js 文件中做了 console.log 语句,它确实到达了正确的位置并且它确实触发了 s3_direct_upload 过程,据我所知,因为我放置了各种 console.log 条目每一步都在里面,我能告诉你每一步都在正常工作。

我也尝试升级到更高版本的 aws-sdk(版本 3)和 kt-paperclip,它做同样的事情所以它似乎不是 paperclip 或 aws 的故障我最好可以告诉。我什至想弄清楚如何手动从数据库中获取下一个 ID,然后在 @attachment.save 之后手动调用 queue_proccessing(我曾尝试这样做)。我无法让它工作的唯一原因是因为 @attachment.save 没有保存,所以它没有返回 ID,没有 ID 我不能手动调用它。

非常感谢任何帮助。我什至愿意雇用或支付某人的帮助,因为我非常困惑并且需要理解这个问题的答案。我有第二个应用程序在相同位置以相同方式失败。这个其他应用程序也是从 Rails v3.2 的更旧版本到当前版本的重写。这是我唯一无法修复的部分。

如果有人想让我 post 任何其他事情请告诉我,我会非常乐意 post 任何其他任何人都可以提供帮助的事情。

谢谢, 斯科特

@attachment.save 试试@attachment.valid?检查对象是否有效 如果对象无效,则通过 @attachment.errors.full_messages 检查错误消息 您可以清楚地看到缺失和无效的属性

这里 " @attachment: #<附件 id: nil, service_id: 1137, user_id: nil, direct_upload_url: "https://mybucket.s3.amazonaws.com/uploads/16365640...", upload_file_name: 无, upload_content_type: 无, upload_file_size: 无, upload_updated_at: 无, 已处理: false, created_at: 无, updated_at:无,当:无>” 您可以看到 user_id 缺少设置用户 ID 然后重试 我希望在此之后你的问题得到解决