上个星期我一直在试图解决这个问题。
我有一个Ruby on Rails应用程序,它有一个表单提交,包含许多文件上传和输入字段。成功提交后,表单信息将保存到Postgres数据库,并使用提交的信息和附件发送电子邮件。如果我禁用验证或者表单没有通过验证,这是正确的,但是如果验证失败则问题就会出现。
如果选择了某个文件,然后单击了提交按钮并且表单验证失败,则某些文件字段将被禁用或从html中完全删除。它还将重新排列原始字段。例如,附加文档是upload49,但在验证失败后,文件字段现在是upload25。
我已经尝试了每个我能想到的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
-@buyer_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: "info@test.com"
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: "testuser@example.com",
subject: "New Buyer Closing File at #{@address} - #{@location}",
reply_to: @user.email
)
end
end
答案 0 :(得分:0)
遇到同样的问题并花了好几天来解决它。但当我发现解决方案时,我有点失望。
如果您正在使用jquery_ujs而不是rails-ujs,请尝试将其更改为application.js中的rails-ujs。
可能为时已晚,但可能会有其他人遇到同样的问题。