【发布时间】: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