Ruby on rails 如何在seeds.rb中引用图像

Ruby on rails 如何在seeds.rb中引用图像,ruby-on-rails,Ruby On Rails,我使用Carrierwave上传与我的用户模型相关联的图像,该模型具有相应的图片属性,该属性在字符串字段中包含图像文件的名称。通常上传图片的名称存储在public/uploads中。现在,我想在我的开发数据库中添加示例用户,包括与概要文件图片相关的路径。我试图将我的pic1.jpg、pic2.jpg、pic3.jpg图片存储在public/images中,并将这些图片引用到db/seeds.rb中作为picture:“/images/pic1.jpg”等,就像Grant Neufeld在一篇文章

我使用Carrierwave上传与我的用户模型相关联的图像,该模型具有相应的图片属性,该属性在字符串字段中包含图像文件的名称。通常上传图片的名称存储在public/uploads中。现在,我想在我的开发数据库中添加示例用户,包括与概要文件图片相关的路径。我试图将我的pic1.jpg、pic2.jpg、pic3.jpg图片存储在public/images中,并将这些图片引用到db/seeds.rb中作为picture:“/images/pic1.jpg”等,就像Grant Neufeld在一篇文章中建议的那样。在视图中,使用以下代码包括图片:

<%= image_tag @user.picture.url if @user.picture? %>

假设您将图像保存在public/images下,Carrierwave要求您将IO对象传递给Rails模型,因此要在seeds.rb文件中正确设置它,您需要执行以下操作:

User.create! 名称:示例用户, 电邮:example@railstutorial.org, 密码:foobar, 密码确认:foobar, 管理员:是的, 政治:"左",, 车:是的, 宠物:“我喜欢马和熊”, 音乐:《美国乡村与民间》, 图片:File.openRails.root.join'public'、'images'、'pic1.jpg', 简介:“我喜欢音乐以及Ruby和Python编程语言”, 是的, 激活时间:Time.zone.now 请注意,我已将图片更改为File.openRails.root.join'public'、'images'、'pic1.jpg'

是否可以包含您的seeds.rb文件?@gmaliar我在原始邮件中包含了我的seeds.rb文件。
User.create!(name:  "Example User",
         email: "example@railstutorial.org",
         password:              "foobar",
         password_confirmation: "foobar",
         admin: true,
         politics: 'left',
         car: true,
         pets: 'I like horses and bears',
         music: 'American country and folk',
         picture: 'pic1.jpg',
         profile: 'I like music and the Ruby and Python programming languages',
         activated: true,
         activated_at: Time.zone.now)

User.create!(name:  "Super-friendly User",
        email: "example-101@railstutorial.org",
        password:              "PassWord-0",
        password_confirmation: "PassWord-0",
        admin: true,
        smoker: true,
        politics: 'left',
        car: true,
        pets: 'I like turtles and whales',
        car_pets: true,
        music: 'Jazz and blues',
        picture: 'pic2.jpg',
        profile: 'I like music and drinking',
        activated: true,
        activated_at: Time.zone.now)

User.create!(name:  "Friendly User",
        email: "example-102@railstutorial.org",
        password:              "PassWord-0",
        password_confirmation: "PassWord-0",
        politics: 'right',
        car: true,
        pets: 'I like snakes and gorillas',
        music: 'pop and classics',
        picture: 'pic3.jpg',
        profile: 'I like music and hiking',
        activated: true,
        activated_at: Time.zone.now)

99.times do |n|
  name  = Faker::Name.name
  email = "example-#{n+1}@railstutorial.org"
  password = "password"
  User.create!(name:  name,
           email: email,
           password:              password,
           password_confirmation: password,
           activated: true,
           activated_at: Time.zone.now)
end