[英]Rails 6, Paperclip, S3, s3_direct_upload @attachment.save not saving after the s3_direct_upload completes
我已經將一個名為 FilterTrak 的應用程序從舊版本的 Rails v4.2 重寫為 Rails v6,現在除了圖片上傳的一部分外,我的所有工作都可以正常工作。
該應用程序使用 Paperclip、aws-sdk-v1 和 s3_direct_upload。 我之前在使用 s3_direct_upload 時遇到了問題。 我現在遇到的問題是在將文件上傳到 s3 后,它應該保存附件,該附件將從數據庫中給我一個 ID,然后調用應該啟動的輔助進程。
由於未知的原因,@attachment.save 沒有保存,並且因為它沒有保存它沒有回調第二個進程,該進程應該將文件移動到 S3 上的永久位置,然后將該信息保存到數據庫中。 我曾嘗試手動調用第二個進程,但由於附件未保存,因此未生成 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
在您單擊“選擇文件”按鈕並選擇您的一個或多個文件后,這是上傳過程的開始,並且再次天氣它是 1 個文件或 10 個文件,它們都成功上傳到 S3 到 /uploads 文件夾中。 進度條出現,構建,完成,然后像預期的那樣消失。 第二個過程永遠不會啟動。
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" = $1 ORDER BY "users"."id" ASC LIMIT $2 [["id", 34], ["LIMIT", 1]]
Customer Load (1.0ms) SELECT "customers".* FROM "customers" WHERE "customers"."id" = $1 LIMIT $2 /*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" = $1 AND "filters"."id" = $2 LIMIT $3 /*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" = $1 AND "services"."id" = $2 LIMIT $3 /*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" = $1 /*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 ($1, $2, $3, $4, $5, $6, $7, $8, $9) 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" = $1 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" = $1 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" = $1, "upload_updated_at" = $2, "processed" = $3, "updated_at" = $4 WHERE "attachments"."id" = $5 [["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" = $1 [["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" = $1 [["id", 990]]
2021-10-25T23:13:28.051630+00:00 app[web.1]: Company Load (0.6ms) SELECT "companies".* FROM "companies" WHERE "companies"."id" = $1 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" = $1 [["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: #<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 = Attachment.create(attachment_params[:id]) 都在@attachment.save 之前,無論我做什么它都不會保存,我只是不明白為什么它不會保存。 舊版本 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 重寫為當前版本。 這是我唯一無法修復的部分。
如果有人想讓我發布任何其他內容,請告訴我,我將非常樂意發布任何其他任何有用的東西。
謝謝你,斯科特
在@attachment.save 上試試@attachment.valid? 檢查對象是否有效 如果對象無效,則通過@attachment.errors.full_messages檢查錯誤消息您可以清楚地看到缺少和無效的屬性
這里“@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>"你可以看到user_id丟失設置用戶id然后再試我希望在這之后你的問題得到解決
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.