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

旧城冷巷雨未停 提交于 2019-12-02 17:14:38

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

user.avatar = File.open(Dir['app/assets/images/*.jpg'].sample)

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!