gpt4 book ai didi

ruby-on-rails - 尝试上传多个文件 - Document(#70285786863740) 预期,当出现 ActionDispatch::Http::UploadedFile(#70285766684260)

转载 作者:行者123 更新时间:2023-12-01 00:59:11 25 4
gpt4 key购买 nike

我正在尝试使用 Dragonfly 一次上传多个文件并存储它们。通过添加 document_uid,我能够成功上传和存储单个文件。和 document_name到我的 case模型,但现在我想创建每个 case 上传多个文件的能力对象,所以我需要我的 document_uiddocument_name在自己的 table 上用FK给了case table 。

我目前收到错误:Document(#70285786863740) expected, got ActionDispatch::Http::UploadedFile(#70285766684260)
应用程序/模型/案例.rb

class Case < ActiveRecord::Base
has_many :documents

attr_accessible :documents
end

class Document < ActiveRecord::Base

belongs_to :case
dragonfly_accessor :document # defines a reader/writer for an uploaded document

attr_accessible :document_uid, :document_name
end

看法
<%= form_for(@case) do |f| %> 
...
<%= f.file_field :documents, :multiple => true %>
...
<% end %>

到目前为止,我的 Controller 中只有默认的 create 方法
 # POST /cases
# POST /cases.json
def create
@case = Case.new(params[:case])

respond_to do |format|
if @case.save
format.html { redirect_to @case, notice: 'Case was successfully created.' }
format.json { render json: @case, status: :created, location: @case }
else
format.html { render action: "new" }
format.json { render json: @case.errors, status: :unprocessable_entity }
end
end
end

参数:
Parameters: {"utf8"=>"✓", "authenticity_token"=>"KAsjoqrQT5HTNKsiH6bu3+MRKB0FKDLdP2Q/Gm9ZYdA=", "case"=>{"documents"=>[#<ActionDispatch::Http::UploadedFile:0x007fd95ebe97d8 @original_filename="Screen Shot 2014-01-25 at 8.59.17 PM.png", @content_type="image/png", @headers="Content-Disposition: form-data; name=\"case[documents][]\"; filename=\"Screen Shot 2014-01-25 at 8.59.17 PM.png\"\r\nContent-Type: image/png\r\n", @tempfile=#<Tempfile:/var/folders/5w/tkmvdtbn2xn98hjy655s67tw0000gn/T/RackMultipart20140727-43998-1go1ez0>>, #<ActionDispatch::Http::UploadedFile:0x007fd95ebe9760 @original_filename="Screen Shot 2014-03-02 at 8.38.22 AM.png", @content_type="image/png", @headers="Content-Disposition: form-data; name=\"case[documents][]\"; filename=\"Screen Shot 2014-03-02 at 8.38.22 AM.png\"\r\nContent-Type: image/png\r\n", @tempfile=#<Tempfile:/var/folders/5w/tkmvdtbn2xn98hjy655s67tw0000gn/T/RackMultipart20140727-43998-1kefqe0>>]}, "button"=>""}

是什么导致了这个错误,我该如何规避它?

最佳答案

我最终不得不做类似的事情:multiple image upload with dragonfly

应用程序/模型/案例.rb

class Case < ActiveRecord::Base
has_many :documents
accepts_nested_attributes_for :documents
attr_accessible :documents
end

class Document < ActiveRecord::Base

belongs_to :case
dragonfly_accessor :document # defines a reader/writer for an uploaded document

attr_accessible :document_uid, :document_name
end

这是我的 Controller 现在的样子:
def create
@case = Case.new(params[:case])

#create the documents from the params
unless params[:docs].nil?
params[:docs].each do |doc|
@case.documents << Document.create(:document => doc.tempfile, :filename => doc.original_filename, :mime_type => doc.content_type, :document_name => doc.original_filename)
end
end

respond_to do |format|
if @case.save
format.html { redirect_to @case, notice: 'Case was successfully created.' }
format.json { render json: @case, status: :created, location: @case }
else
format.html { render action: "new" }
format.json { render json: @case.errors, status: :unprocessable_entity }
end
end
end

看法:
<%= form_for(@case) do |f| %> 
...
<%= f.file_field :documents, :name => 'docs[]', :multiple => true %>
...
<% end %>

基本上我被迫手动保存文档,而不是通过表单助手自动保存。

关于ruby-on-rails - 尝试上传多个文件 - Document(#70285786863740) 预期,当出现 ActionDispatch::Http::UploadedFile(#70285766684260),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24988265/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com