[英]Carrierwave multi-file upload Rails 5 not working
In my rails app, I wish for the users to be able to upload multiple files at once. 在我的Rails应用程序中,我希望用户能够一次上传多个文件。
I am using the carrierwave gem 我正在使用载波宝石
gem 'carrierwave', github: 'carrierwaveuploader/carrierwave'
On the master branch which supposedly supports the multiple: true
在主分支上,该分支应该支持
multiple: true
found out in my internet searching desperation about the necessary addition of optional: true
in my model for my attachment. 在我的互联网搜索中发现了有关
optional: true
的必要添加的绝望optional: true
在我的附件模型中为optional: true
。
I'm using mysql so I can't use an array type in my database, but I've set up a has_many
, belongs_to
relationship between Request
and Request_Attachment
in my database. 我正在使用mysql,因此无法在数据库中使用数组类型,但是我已经在数据库中设置了
has_many
, Request
和Request_Attachment
之间的belongs_to
关系。
my form: 我的表格:
<%= f.fields_for :request_attachments do |ra| %>
<div class="row" id="uploader">
<div class="col-xs-12">
<label class="btn btn-info"> Upload Files
<%= ra.file_field :file, multiple: true, name: "request_attachments[file][]", :style => "display: none" %>
</label>
</div>
</div>
<% end %>
my controller 我的控制器
@request = Request.new(request_params)
if @request.save
if params[:request][:request_attachments]
params[:request][:request_attachments]['file'].each do |f|
@request_attachment = @request.request_attachments.create!(:file => f)
end
end
flash[:success] = "Your request was submitted successfully, check your email for a confirmation message."
redirect_to action: 'index', status: 303
else
render :new
end
def request_params
params.require(:request).permit(:jobtitle, :requester, :status, request_attachments_attributes: [:id, :request_id, :file])
end
my models: 我的模特:
Request: 请求:
class Request < ApplicationRecord
has_many :request_attachments
accepts_nested_attributes_for :request_attachments
end
Request_Attachments: Request_Attachments:
class RequestAttachment < ApplicationRecord
mount_uploader :file, FileUploader
belongs_to :request, optional: true
end
I have tried variations of the form, like taking out the name
portion. 我尝试过形式的变体,例如去掉
name
部分。
When I remove the multiple: true
portion, it will work perfectly, but it does not work with the multiple
option. 当我删除“
multiple: true
部分时,它会完美运行,但不适用于“ multiple
选项。
I'm not quite sure where the issue may be, so any help would be great. 我不太确定问题可能在哪里,因此任何帮助都将非常有用。
What is happening now is that the request gets saved, but 现在发生的事情是请求被保存,但是
Only 1 request_attachment is created, and 仅创建了1个request_attachment,并且
The filename of the request_attachment is nil
request_attachment的文件名为
nil
Okay, I think I figured it out. 好吧,我想我明白了。
I made a couple changes, which threw an error, and I looked closely at the Parameter Hash, and I noticed essentially it looked like this: 我进行了一些更改,这引发了错误,并且我仔细查看了“参数哈希”,并发现实际上它看起来像这样:
{"utf8"=>"✓",
"request"=>
{"jobtitle"=>"Testing, again and again, and again",
"request_attachments_attributes"=>
{"0"=>
{"file"=>
[#<ActionDispatch::Http::UploadedFile:0x007fee9df806b8
@content_type="image/gif",
@headers="Content-Disposition: form-data; name=\"blah\"; filename=\"logo1.gif\"\r\nContent-Type: image/gif\r\n",
@original_filename="logo1.gif",
@tempfile=blah,
#<ActionDispatch::Http::UploadedFile:0x007fee9df80690
@content_type="image\gif",
@headers=
"Content-Disposition: form-data; name=\"blah\"; filename=\"Blah.gif\"\r\nContent-Type: image/gif\r\n",
@original_filename="Blah.gif",
@tempfile=blahblah]}}
I noticed the "0"=>
and thought "wait.... what is that doing there?.... 我注意到
"0"=>
并以为“等等。。。那是做什么的?。
So I removed the name
section in my form, and changed my controller to look like this: 因此,我删除了表单中的
name
部分,并更改了控制器,使其看起来像这样:
if @request.save
params[:request][:request_attachments_attributes]["0"]['file'].each do |f|
@request_attachment = @request.request_attachments.create!(:file => f)
end
And that seemed to have worked. 这似乎奏效了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.