2014-07-26 38 views
1

我如何使用载波导航上传填充导轨灯具(yaml)? 该文档似乎没有涵盖这一点,而carrierwave wiki也没有。Carrierwave和导轨灯具

我试图

和我已验证的是,上述代码红宝石生成一个有效的文件对象。

+0

因为不鼓励他们,所以在Ruby on Rails文档中不再涉及固定装置。存在更有效的创建初始数据的方法,例如工厂。看到[FactoryGirl](http://github.comthoughtbot/factory_girl) – tralston

+0

好的Rails 4仍然捆绑在一起。你是否说文档编写者和框架开发者之间有分歧? – Edward

+0

我不知道它仍然捆绑的原因,向后兼容性,我想?在Google上搜索“rails fixtures vs factories”,你会看到社区的共识是不使用灯具。请参见[此回复](http://stackoverflow.com/a/992304/695772) – tralston

回答

-1

我怀疑夹具随处可见,因为它们专门用于Rails代码库本身。另外我使用它们,所以这是重要的。

我也发现这article帮助我,也是你。

归结于test_helper.rb

class ActiveSupport::TestCase 
    # Add more helper methods to be used by all tests here... 
    CarrierWave.root = Rails.root.join('test/fixtures/files') #path is up to you 
    def after_teardown 
    super 
    CarrierWave.clean_cached_files!(0) 
    end 
end 

class CarrierWave::Mount::Mounter 
    def store! 
    # Not storing uploads in the tests 
    end 
end 

然后我把我的上传:

def store_dir 
    if Rails.env.test? 
    "images" 
    else 
    "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}" 
    end 
end 

我敢肯定,我可以摆脱需要一种store_dir三元类似声明,但现在有用。如果这对你不起作用,我还有另一种方式,但这样更清洁。