【问题标题】:Seeding file uploads with CarrierWave, Rails 3使用 CarrierWave、Rails 3 播种文件上传
【发布时间】:2011-04-24 00:50:34
【问题描述】:

我正在尝试使用 CarrierWave 为 Rails 3 中的数据库播种图像,但我尝试的任何操作似乎都必须手动上传。

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!

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

【问题讨论】:

  • 对不起,如果我离题了,'pi' 是保留字吗?
  • 不,ruby 中的 pi 表示为常量,区分大小写为 PI。尽管如此,有趣的想法。 ;]
  • 顺便说一句,那是Math::PI。 ;]

标签: ruby-on-rails ruby rake carrierwave seed


【解决方案1】:

原来 CarrierWave 的文档有点错误。还有一段更新的代码in the README at the GitHub repository for the project

不过,简而言之:

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

【讨论】:

  • 如果需要,也可以在一行中执行此操作:ProductImage.create!(:product =&gt; product, image: File.open(File.join(Rails.root, 'test.jpg')))
  • 也可以一行完成整个动作product.product_images.create!(image: File.open(File.join(Rails.root, 'test.jpg')))
【解决方案2】:

只要您的上传器已安装到您的模型,使用 mount_uploader 方法,您就可以使用相关的 open 方法为您的模型播种载波。这将是实现相同目标的更简洁的方式。就我而言,我是从 URL 播种的:

Game.create([
{
  :title => "Title",
  :uuid_old => "1e5e5822-28a1-11e0-91fa-0800200c9a66", 
  :link_href => "link_href", 
  :icon => open("http://feed.namespace.com/icon/lcgol.png"),
  :updated_at => "2011-01-25 16:38:46", 
  :platforms => Platform.where("name = 'iPhone'"), 
  :summary => "Blah, blah, blah...", 
  :feed_position => 0, 
  :languages => Language.where("code = 'de'"), 
  :tags => Tag.where(:name => ['LCGOL', 'TR', 'action'])
},
{...

【讨论】:

  • 可以提供open,而不是remote_[attribut name]_url
【解决方案3】:

这是我为我的一个项目合并到 seed.rb 文件中的示例脚本。 我确信它可以改进,但它提供了一个很好的工作示例。

我提取的所有资产都存储在 app/assets/images 中,它们的名称与我的 Info 对象的名称匹配(在我用下划线替换空格并将名称小写之后)。

是的,这听起来确实效率低下,但除了将这些资产放在某个地方的 FTP 上之外,这是我为远程服务器找到的最佳解决方案,它能够使用 Carrierwave 和 Fog 将文件直接上传到 S3。

My Info 模型与 Gallery 模型具有 has_one 关联,而 Gallery 模型与 Photo 模型具有 has_many 关联。 Carrierwave 上传器安装在该模型的“文件”(字符串)列上。

Info.all.each do |info|              
  info_name = info.name.downcase.gsub(' ', '_')
  directory = File.join(Rails.root, "app/assets/images/infos/stock/#{info_name}")

  # making sure the directory for this service exists
  if File.directory?(directory)
    gallery = info.create_gallery

    Dir.foreach(directory) do |item|
      next if item == '.' or item == '..'
      # do work on real items
      image = Photo.create!(gallery_id: gallery.id)
      image.file.store!(File.open(File.join(directory, item)))
      gallery.photos << image
    end

    info.save!

  end
end

这对我来说完美无缺,但理想情况下,我不必将要上传到 S3 的文件打包到 assets 文件夹中。我非常愿意接受建议和改进。

【讨论】:

    【解决方案4】:

    这一切都在文档中:https://github.com/carrierwaveuploader/carrierwave/wiki/How-to:-%22Upload%22-from-a-local-file

    restaurant = Restaurant.create!(name: "McDonald's")
    restaurant.logo = Rails.root.join("db/images/mcdonalds_logo.png").open
    restaurant.save!
    

    【讨论】:

      【解决方案5】:

      基于@joseph jaber 的评论,这对我来说是一种享受:

      下面的代码应该在seeds.rb

      20.times do
              User.create!(
                  name: "John Smith",
                  email: "john@gmail.com",
                  remote_avatar_url: (Faker::Avatar.image)
              )
          end
      

      这将创建 20 个用户并为每个用户分配不同的头像。

      我使用了 faker gem 来生成数据,但 Faker::Avatar.image 所做的只是返回一个标准 url,所以你可以使用你选择的任何 url。

      上面的示例假设您存储图像的用户模型属性称为avatar

      如果属性被称为图像,你会这样写:

      remote_image_url: (Faker::Avatar.image)

      【讨论】:

        【解决方案6】:

        对我来说最简单的解决方案是:

        1. 注释掉模型中的 mount_uploader 行
        2. 播种数据
        3. 取消注释模型中的行

        【讨论】:

        • 取消注释模型上传器后,挂载字段的文件 attr 为 nil
        猜你喜欢
        • 2016-04-05
        • 1970-01-01
        • 2013-11-20
        • 1970-01-01
        • 1970-01-01
        • 2013-06-30
        • 2018-06-23
        • 2013-02-24
        • 2011-10-06
        相关资源
        最近更新 更多