Answer the question
In order to leave comments, you need to log in
How to upload multiple files with one request in CarrierWave/Sinatra?
I'm not the first to ask, but I won't be the last either. How to implement multiple file upload in CarrierWave and Sinatra? I have this action:
post '/create' do
params.delete 'submit'
d = Dcmnt.new(
:published => params[:published],
:name => params[:name],
:description => params[:description],
:created_at => Time.now
)
d.attachment = params[:attachments]
d.save
redirect '/success'
end
class Dcmnt
include Mongoid::Document
store_in collection: 'dcmnts'
field :published, type: Boolean
field :name, type: String
field :description, type: String
field :created_at, type: Date
mount_uploader :attachment, Uploader, type: String
end
%form{:method => "post", :action => "/create", :enctype => "multipart/form-data"}
%input{:type => "checkbox", :name => "published", :value => "true"} Published?
%input{:name => "name"}
%textarea{:name => "description", :rows => "5"}
%div.form-group
%label Attachments
%input{:type => "file", :name => "attachments[]"}
%input{:type => "file", :name => "attachments[]"}
%input{:type => "file", :name => "attachments[]"}
%input{:type => "file", :name => "attachments[]"}
%input{:type => "file", :name => "attachments[]"}
class Uploader < CarrierWave::Uploader::Base
storage :file
def store_dir
'attachments/' + model.id
end
end
Answer the question
In order to leave comments, you need to log in
You're a little confused about how Carrierwave works. In the future, to solve such problems, it makes sense to read the source code of the library you use. The mount_uploader :attachment method adds a method to the model
def attachment=(blabla)
...
end
class Dcmnt
include Mongoid::Document
...
has_many :attachments
accepts_nested_attributes_for :attachments
end
class Attachment
include Mongoid::Document
...
belongs_to :dcmnt
mount_uploader :file, Uploader
end
d = Dcmnt.new(
:published => params[:published],
:name => params[:name],
:description => params[:description],
:created_at => Time.now
)
d.attachment = params[:attachments]
d = Dcmnt.new(
:published => params[:published],
:name => params[:name],
:description => params[:description],
:attachments_attributes => params[:attachments_attributes]
:created_at => Time.now
)
:created_at => Time.now
you can add to the model include Mongoid::Timestamps::Created
%div.form-group
%label Attachments
%input{:type => "file", :name => "attachments_attributes[0][file]"}
%input{:type => "file", :name => "attachments_attributes[1][file]"}
%input{:type => "file", :name => "attachments_attributes[2][file]"}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question