I'm trying to use Dragonfly to upload multiple files at a time and store them. I was able to successfully upload and store a single file by adding a document_uid
and document_name
to my case
model, but now I want to create the ability to upload multiple files per case
object so I need to have my document_uid
and document_name
in their own table with a FK to the case
table.
I'm currently getting the error: Document(#70285786863740) expected, got ActionDispatch::Http::UploadedFile(#70285766684260)
app/models/case.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
view
<%= form_for(@case) do |f| %>
...
<%= f.file_field :documents, :multiple => true %>
...
<% end %>
So far i just have the default create method in my controller
# 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
params:
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"=>""}
What is causing this error and how can I circumvent it?
I ended up having to something similar to this: multiple image upload with dragonfly
app/models/case.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
Here's what my controller now looks like:
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
View:
<%= form_for(@case) do |f| %>
...
<%= f.file_field :documents, :name => 'docs[]', :multiple => true %>
...
<% end %>
Basically i'm being forced to save the documents manually rather than automatically through the form helper.