【问题标题】:Dropzone on Rails causing double form submission to DBRails 上的 Dropzone 导致向 DB 提交双重表单
【发布时间】:2016-04-21 20:40:52
【问题描述】:

单击“保存”按钮时,会创建两个条目。一个存储了 :map 信息,一个缺少它,两者都具有相同的 :name。如何只获取完整的条目来保存?

new.html.erb

<%=form_for [@location, @floor], html: {class: "dropzone", multipart: true, id: "map-upload"} do |f|%>
  <div>
    <%=f.label :name%>
    <%=f.text_field :name%>
  </div>

  <div class="fallback">
    <%=f.label :map%>
    <%=f.file_field :map%>
  </div>

  <%=f.submit "Save", id: "floor-submit"%>
<%end%>

拖放.js

$(document).ready(function() {
  // disable auto discover
  Dropzone.autoDiscover = false;

  var mapUpload = new Dropzone("#map-upload", {
    paramName: "floor[map]",
    autoProcessQueue: false,
    maxFiles: 1,
    addRemoveLinks: false,
    dictDefaultMessage: 'Drag & drop a map image file here, or click here to select file to upload',
  });

  $('#map-upload').submit(function(e){
    mapUpload.processQueue();
  });
});

floor_controller 的相关部分:

  def create
    @floor = current_user.company.locations.find(params[:location_id]).floors.create(floor_params)

    if @floor.save
      flash[:notice] = "Floor has been created."
      redirect_to location_floors_path
    else
      flash[:alert] = "There was an error saving the floor, please try again"
      render 'new'
    end
  end

型号

class Floor < ActiveRecord::Base
  belongs_to :location

  has_attached_file :map
  validates_attachment_content_type :map, :content_type => /\Aimage\/.*\Z/
end

【问题讨论】:

    标签: jquery ruby-on-rails forms paperclip dropzone.js


    【解决方案1】:

    我认为您的 js 提交了一次,而您的 rails 表单正在提交第二次。建议禁用其中之一。

    【讨论】:

    • 我同意。如果我在表单提交上 .preventDefault() ,那么我无法重定向回索引。如果我取消 .processQueue() 那么图像将不会上传。这就是我的困境。
    • 我认为,如果您确实阻止了表单上的默认设置,您仍然只是从您的 js 添加重定向到索引操作。例如location.href = /index 在您的进程队列之后。
    • 对不起,我的意思是重定向到我的 rails 控制器的 index 方法。它需要来自用户帐户的参数,所以我不能通过 jQuery 进行简单的重定向(或者我没有找到)。
    • 将参数与您的 js 请求一起传递? stackoverflow.com/questions/14846626/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-10-01
    • 2012-04-05
    • 2021-09-02
    • 2018-07-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多