Rails上的Dropzone导致向DB提交双表单

时间:2016-04-21 20:40:53

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

当" Save"单击按钮,创建了两个条目。一个用:存储地图信息,一个丢失它,两者都有相同的名称。我如何只获得完整的保存条目?

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%>

拖放drop.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

1 个答案:

答案 0 :(得分:0)

我认为你的js正在提交一次,你的rails表单第二次提交它。建议禁用其中一个。