我使用CarrierWave上传与我的用户模型相关联的图像,它有一个对应的
picture
在字符串字段中包含图像文件名的属性。通常上传图片的名称存储在
public/uploads
.
现在,我想为我的开发数据库植入示例用户,包括配置文件图片的关联路径。我试着储存我的
pic1.jpg
,
pic2.jpg
,
pic3.jpg
中的图片
public/images
参考这些图片
db/seeds.rb
作为
picture: '/images/pic1.jpg'
等等,就像格兰特·纽菲尔德在
old stackoverflow question
. 在视图中,使用以下代码包含图片:
<%= image_tag @user.picture.url if @user.picture? %>
但是这不起作用,因为picture属性为nil,所以图像不会加载到视图中。我也试着把我的照片储存在
app/assets/images
并将其引用为
picture: 'pic1.jpg'
,
picture: 'pic2.jpg'
,
picture: 'pic3.jpg'
在里面
db/种子.rb
没有结果。
下面是我的
db/种子.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