2016-04-21 61 views
0

单击“保存”按钮时,会创建两个条目。一个带有:地图信息存储,一个缺少它,两者都有相同的名称。我怎样才能得到完整的条目来保存?Dropzone on Rails导致双向表单提交到数据库

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 

回答

0

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

+0

我同意。如果我在表单提交.preventDefault()提交然后我无法重定向回到索引。如果我取消.processQueue(),那么图像将不会上传。这就是我的困境。 –

+0

我想如果你确实阻止了表单上的默认值,你仍然只是从你的js中添加一个重定向到索引操作。例如location.href =/index你的processqueue之后。 –

+0

对不起,我的意思是重定向到我的轨道控制器的索引方法。它需要从用户帐户param,所以我不能做一个简单的重定向通过jQuery(或不是我发现)。 –