2016-04-21 51 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(或不是我發現)。 –