我面临一个奇怪的问题。
我有Post has_many attachments. as: :imageable
和
Attachment belongs_to imageable, polymorphic: true
我正在尝试使用dropzone.js上传附加到单个帖子的多个图片
但是,每当我以dropzone驱动的形式上传多个文件时,每个图像都会作为自己的帖子上传。我上传了4张图片,每张附图都有4张帖子。 如何才能将我附加到帖子的所有图片仅与该帖子相关联?
以下是posts.js
的样子:
$(document).ready(function(){
// disable auto discover
Dropzone.autoDiscover = false;
// grap our upload form by its id
$("#new_post").dropzone({
// restrict image size to a maximum 1MB
maxFilesize: 1,
// changed the passed param to one accepted by
// our rails app
paramName: "post[attachments_attributes][][picture]",
// show remove links on each image upload
addRemoveLinks: true
});
});
从PostsController
def create
@post = Post.create(post_params)
if @post.save
render json: { message: "success" }, :status => 200
else
# you need to send an error header, otherwise Dropzone
# will not interpret the response as an error:
render json: { error: @post.errors.full_messages.join(',')}, :status => 400
end
end
_form.html.erb
发布后new
的相关部分:
<%= form_for(@post, html: {class:"dropzone"}) do |f| %>
....
<div class="fallback">
<%= f.label :attachments %>
<%= f.fields_for :attachments do |at| %>
<%= at.file_field :picture %>
<% end %>
</div>
....
<% end %>
参数名称为post[attachment_attributes][0][picture]
,因为它显示在HTML表单中。
我怀疑这是问题的原因,但我如何更改它以便请求上传附加到一个帖子的所有图像?
提前致谢!