如何通过CarrierWave和Sinatra进行多个文件上传?

时间:2014-05-14 02:03:34

标签: ruby sinatra carrierwave

我不是第一个问谁,也许不是最后一个。如何在Sinatra中使用CarrierWave实现多次上传?我正在使用此代码:

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[]"}

CW的配置:

class Uploader < CarrierWave::Uploader::Base
  storage :file
  def store_dir
    'attachments/' + model.id
  end
end

看起来不错,但不起作用。当我尝试将单个文件上传到几个文件时,Pow会在 d.attachment = params [:attachments] 行返回没有将nil隐式转换为字符串。我无法弄清楚原因。你能帮我解决这个问题吗?

0 个答案:

没有答案