请帮助我了解CarrierWave的工作原理。 我正在使用最小的Sinatra / DataMapper应用程序,其中包含以下内容:
class VideoUploader < CarrierWave::Uploader::Base
storage :file
end
class Video
include DataMapper::Resource
property :id, Serial
property :name, String
property :desc, Text
mount_uploader :file, VideoUploader
end
get '/' do
slim :form
end
post '/' do
video = Video.new
video.name = params[:name]
video.desc = params[:desc]
video.file = params[:file]
video.save
redirect '/'
end
据我所知,视频定义中的mount_uploader :file, VideoUploader
字符串将 .video 方法添加到视频实例,我可以存储上传分配 params [:file] 。当我尝试从浏览器发送表单时,请求在DB表中成功创建了记录,但我在DB和 public_directory 中找不到任何文件存在的迹象。我做错了什么?
答案 0 :(得分:0)
您可能应该在store_dir
类中定义VideoUploader
方法:
class VideoUploader < CarrierWave::Uploader::Base
storage :file
# Override the directory where uploaded files will be stored.
# This is a sensible default for uploaders that are meant to be mounted:
def store_dir
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
....
end