【问题标题】:Faker Gem creating fake picture not workingFaker Gem 创建假图片不起作用
【发布时间】:2015-08-27 00:33:13
【问题描述】:

为什么我不能创建假图片?

图片是多态的,包括模型提案:

这是我的伪造数据:

for n in 1..10 do
  picture = Picture.create!(
    picture: Faker::Avatar.image("my-own-slug"),
    imageable_type: 'Proposal',
    imageable_id: n)
end

图像类型和图像 ID 正确保存,我可以通过控制台验证。但我的图片字段仍然为零

picture: nil

如果只是在我的控制台中运行Faker::Avatar.image("my-own-slug"),我会得到:

"http://robohash.org/my-own-slug.png?size=300x300&set=set1"

这是我正在寻找的。为什么我的种子不起作用?

我正在播种很多其他数据,除了图像之外,这一切都与 Faker 配合得很好。我错过了什么吗?

【问题讨论】:

    标签: ruby-on-rails ruby database ruby-on-rails-4 faker


    【解决方案1】:

    我最终解决了这个问题,方法是将图像保存到我的根文件夹,然后将其显式 url 播种到我的根文件夹:

    for n in 1..10 do
      picture = Picture.create!(
        picture: File.open(File.join(Rails.root, "Joan_Baez_Bob_Dylan_crop.jpg")),
        imageable_type: 'Proposal',
        imageable_id: n)
    end
    

    【讨论】:

    • 由于在传递字符串(来自 Faker::Avatar.image)时将文件传递到图片属性不起作用,我猜图片模型正在使用回形针(或其他文件上传gem),并且图片设置器期望接收文件而不是字符串。
    猜你喜欢
    • 2019-06-18
    • 2017-04-08
    • 1970-01-01
    • 2018-10-29
    • 1970-01-01
    • 2021-02-11
    • 2014-04-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多