Carrierwave 默认接收store_dir
上传器生成的 url,并将路径添加到 rails 应用程序的公共文件夹并存储文件。
例如,如果
def store_dir
"uploads/#{model.id}"
end
然后文件存储在public/uploads/:attachment_id
如果尝试将存储的文件移出公用文件夹,它仍然保存在公用文件夹中。有谁知道如何将文件存储在公用文件夹之外?
Carrierwave 默认接收store_dir
上传器生成的 url,并将路径添加到 rails 应用程序的公共文件夹并存储文件。
例如,如果
def store_dir
"uploads/#{model.id}"
end
然后文件存储在public/uploads/:attachment_id
如果尝试将存储的文件移出公用文件夹,它仍然保存在公用文件夹中。有谁知道如何将文件存储在公用文件夹之外?
最干净的方法是设置 CarrierWave 根选项
CarrierWave.configure do |config|
config.root = Rails.root
end
然后store_dir
将在此根目录中使用。
我意识到这不是一个真正的当前问题,但我偶然发现它正在寻找其他东西。答案就是使用 Rails.root,例如:
def store_dir
"#{Rails.root}/private/files/#{model.id}"
end
在商店目录中,您还可以执行以下操作:
def store_dir
"#{Rails.root.join('public', 'system', 'uploads')}/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
更改 config_root 的解决方案对我不起作用。
如果有人只为 RSpec 需要它,那么就做
describe SomeClass do
before do
CarrierWave.stub(:root).
and_return(Pathname.new "#{Rails.root}/tmp/fake_public")
end
it { ... }
end
如果你想要所有的测试
# spec/spec_helper.rb
RSpec.configure do |config|
# ...
config.before :each do
# ...
CarrierWave.stub(:root).and_return(Pathname.new "#{Rails.root}/tmp/public")
end
end