使用CarrierWave,Rails 3上传种子文件 [英] Seeding file uploads with CarrierWave, Rails 3

查看:63
本文介绍了使用CarrierWave,Rails 3上传种子文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用CarrierWave在Rails 3中使用图像播种数据库,但是除了尝试手动上传所有图像外,我尝试的其他方法似乎无济于事。

  pi = ProductImage.new(:product =>产品)
pi.image = File.open(File.join(Rails.root,'test.jpg')))
pi.store_image! #尝试使用和不使用此
产品。product_images<< pi
product.save!

有人知道如何使用CarrierWave进行播种吗?

解决方案

结果证明,CarrierWave的文档略有错误。在项目的GitHub存储库中的自述文件中,有一个最新的代码

总而言之:

  pi = ProductImage.create!(:product => product)
pi.image.store!(File.open(File.join(Rails.root,'test.jpg')))
product.product_images<< pi
product.save!


I'm trying to seed a database in Rails 3 with images using CarrierWave, however nothing I try seems to work short of having to upload them all by hand.

pi = ProductImage.new(:product => product)
pi.image = File.open(File.join(Rails.root, 'test.jpg'))
pi.store_image! # tried with and without this
product.product_images << pi
product.save!

Anybody know how to seed using CarrierWave at all?

Turns out the documentation for CarrierWave is slightly wrong. There is a more up to date piece of code in the README at the GitHub repository for the project.

In a nutshell, though:

pi = ProductImage.create!(:product => product)
pi.image.store!(File.open(File.join(Rails.root, 'test.jpg')))
product.product_images << pi
product.save!

这篇关于使用CarrierWave,Rails 3上传种子文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆