Generating Paperclip image uploads with fake data - Ruby on Rails Populator / Faker Gems

后端 未结 3 908
孤独总比滥情好
孤独总比滥情好 2021-01-31 10:41

I am currently trying to populate a development database on a project with a bunch of fake data, to simulate how it will look and operate with hundreds of articles / users. I lo

相关标签:
3条回答
  • 2021-01-31 11:28

    One way I get around this is to put a conditional in my views.

    Let's say your model is "user", and it has an avatar. Then you can do the following:

    <% if product.avatar.exists? %>
      ... show the attached photo
    <% else %>
      .. show a default photo
    <% end %>
    

    This works for me with Paperclip, and I use it in my dev database all the time rather than worrying about having all the image attached to all the users.

    0 讨论(0)
  • 2021-01-31 11:31
    user.avatar = File.open(Dir['app/assets/images/*.jpg'].sample)
    
    0 讨论(0)
  • 2021-01-31 11:32

    To associate a random image in your task, you could try the following:

    user.avatar = File.open(Dir.glob(File.join(Rails.root, 'sampleimages', '*')).sample)
    

    where sampleimages is a directory containing avatars to be associated at random

    0 讨论(0)
提交回复
热议问题