我在上週試圖解決這個問題的過程中一直在敲牆。Ruby on Rail,Carrierwave - 文件上傳字段在表單驗證失敗時消失
我有一個Ruby on Rails應用程序,它有一個表單提交,有許多文件上傳以及輸入字段。成功提交後,表單信息將保存到Postgres數據庫中,並隨提交的信息和附加文件一起發送電子郵件。如果我禁用了驗證,或者表單沒有失敗驗證,那麼這是正確的,但是如果驗證失敗,問題就會出現。
如果選擇了文件,然後將點擊提交按鈕和形式失敗驗證的一些文件中的字段將或者通過禁用或從HTML完全刪除。它也將重新排列原始字段的字段。例如其他文檔上傳49,但驗證失敗後,文件字段現在上傳25。
我已經嘗試過所有可以用來追蹤問題的Google搜索。我發現的最接近的問題是AJAX和文件上傳的問題,出於安全原因,默認情況下是不允許的,這就是Carrierwave是必需的。我注意到,當發生錯誤'ajax:aborted:file'時,這是刪除文件字段的原因。我發現的另一件事是,當推送提交按鈕並且正在處理表單時,所有文件字段都被臨時禁用,我只能假設某處發生了某些事情正在阻止它們在驗證失敗時被重新啓用。
如果我爲'ajax:aborted:file'添加了一個偵聽器,並讓它返回false,它將阻止文件字段被搞亂,但它會導致表單提交和驗證停止工作。
任何幫助將不勝感激。如果您有任何問題或需要更多代碼,請告訴我。
以下是一些代碼片段。
# uploaders/AttachmentsUpload.rb
class AttachmentsUploader < CarrierWave::Uploader::Base
# Choose what kind of storage to use for this uploader:
# storage :fog
storage :file # for local testing, saving to S3 in production
# Override the directory where uploaded files will be stored.
# This is a sensible default for uploaders that are meant to be mounted:
def store_dir
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
end
# models/buyer_under_contract.rb
class BuyerUnderContract < ApplicationRecord
include AttributeOption
belongs_to :user
belongs_to :team
mount_uploader :upload1, AttachmentsUploader
mount_uploader :upload2, AttachmentsUploader
mount_uploader :upload3, AttachmentsUploader
mount_uploader :upload4, AttachmentsUploader
mount_uploader :upload5, AttachmentsUploader
mount_uploader :upload6, AttachmentsUploader
...
...
mount_uploaders :upload49, AttachmentsUploader
after_create :send_notification
# Agent Info Validations
validates :location, :agent_fname, :agent_lname,
:agent_email,
presence: true
validates :prior_contract,
presence: true,
if: :prior_contract_status?
validates :previous_transaction_coordinator,
presence: true,
if: :previous_contract?
....
....
....
def send_notification
NotificationMailer.buyer_under_contract_form(self).deliver
end
end
# controllers/buyer_under_contracts.rb
class BuyerUnderContractsController < ApplicationController
include ListItemsController
responders :ajax_modal, :collection
respond_to :js, only: %i[index new create edit update]
respond_to :html, only: [:index]
load_and_authorize_resource
before_action :restrict_users, only: %i[new create edit update]
def new
list_item.user = current_user
respond_with(list_item)
end
def create
puts '@create' + list_item.inspect
created = list_item.save
if created
redirect_to URI(request.referer).path
return
end
puts '@errors' + list_item.errors.inspect
respond_with(list_item)
end
def update
updated = list_item.update_attributes(resource_params)
return if resource_params[:_update_type] == 'inline'
if updated
target = URI(request.referer).to_s
redirect_to target
return
end
respond_with(list_item)
end
def buyer_under_contract_params
numeric = %i[location zip mls purchase_price broker_commission_percentage
broker_commission_fee buyer_transaction_fee]
params[:buyer_under_contract].each do |param, value|
if param.to_sym == :communication_pref
value.delete_if { |x| x == '' }
params[:buyer_under_contract][param] = value.join(', ')
end
if param.to_sym == :documents_present
value.delete_if { |x| x == '' }
params[:buyer_under_contract][param] = value.join(', ')
end
next unless numeric.include?(param.to_sym)
params[:buyer_under_contract][param] = value.gsub(/[^0-9.]/, '')
end
# I know this is bad practice, was having problems with
# unpermitted parameters even though I permitted them,
# just for testing now.
params.require(:buyer_under_contract).permit!
end
# views/_form.html.haml
= simple_form_for(@buyer_under_contract, remote: true) do |form|
-if @buyer_under_contract.errors.any?
.modal-header.error-container
[email protected]_under_contract.errors.messages.each do |attr, _msg|
.row
=I18n.t(:"simple_form.labels.buyer_under_contract.#{attr}") + " is required"
.modal-body.container
%div.hidden.container
= form.input :user_id, value: User
%div.agent_info.container
%h2 Agent Information
%div.container
%h3.form *Location
= form.input :location, placeholder:'Location', label:false, collection: Team.all, prompt: 'Location', wrapper_html: {class: 'col-sm-5'}, input_html: { data: { placeholder: 'Location', 'allow-clear': false } }
Select the city your office is located in
%div.container
%h3.form Agent Name (Your Name)
= form.input :agent_fname, label:'First', wrapper_html: { class: 'col-sm-5' }
= form.input :agent_lname, label:'Last', wrapper_html: { class: 'col-sm-5' }
...
...
...
%div.documents.container(id = 'doc_uploads')
%h3.form Documents Upload(s)
%div.container.hidden(id = 'doc_addendum_purchase_agree_arbitration')
%h5 Addendum to Purchase Agreement/Arbitration
= form.file_field :upload1, multiple: false, wrapper_html: { class: 'col-sm-6'}
%div.container.hidden(id = 'doc_addendum_purchase_agree_cic')
%h5 Addendum to Purchase Agreement/CIC (if applies)
= form.file_field :upload2, multiple: false, wrapper_html: { class: 'col-sm-6'}
%div.container.hidden(id = 'doc_addendum_purchase_agree_inspect')
%h5 Addendum to Purchase Agreement/Inspections
= form.file_field :upload3, multiple: false, wrapper_html: { class: 'col-sm-6'}
...
...
%div.container.hidden(id = 'doc_additional')
%h5 Additional Documents
= form.file_field :upload49, multiple: true, wrapper_html: { class: 'col-sm-6'}
.modal-footer
= form.button :submit, class: "btn btn-success contract_submit_button", data: {disable_with: "Please wait…"}, value: 'Submit'
= link_to "Cancel", "#", class: "btn btn-danger", data: {dismiss: "modal"}, type: "button"
# assets/buyer_under_contract.js.coffee
# Some complicated JS to hide and unhide fields, but no
# disabling of fields, I can attach if it's needed
# mailers/notification_mailer.rb
class NotificationMailer < ApplicationMailer
default from: "[email protected]"
def buyer_under_contract_form(submission)
@submission = submission
@user = User.find_by_id(@submission.user_id)
@address = @submission.address
@location = Team.find_by_id(@submission.location)
puts 'address: ' + @address.to_s
puts 'user: ' + @user.to_s
puts 'location: ' + @location.to_s
attachments.inline['white-logo.png'] = File.read('app/assets/images/white-logo.png')
(1..49).to_a.each do |value|
begin
if @submission.send("upload#{value}").is_a?(Array)
@submission.send("upload#{value}").each do |attachment|
file_url = attachment.to_s
next if file_url == ''
puts 'file url: ' + file_url
puts "#{Rails.root}/public/#{file_url}"
attachments.inline[attachment.filename] = File.read("#{Rails.root}/public/#{file_url}")
end
else
file_url = @submission.send("upload#{value}").to_s
next if file_url == ''
puts 'file url: ' + file_url
puts "#{Rails.root}/public/#{file_url}"
attachments.inline[@submission.send("upload#{value}").filename] = File.read("#{Rails.root}/public/#{file_url}")
end
rescue
puts 'Mailer/Attachment Failure'
puts $!.message
next
end
end
mail(to: "[email protected]",
subject: "New Buyer Closing File at #{@address} - #{@location}",
reply_to: @user.email
)
end
end