【发布时间】:2020-08-26 20:18:32
【问题描述】:
我正在通过他的种子文件填充一个 Rails 应用程序,并想为每个帐户附加一张图片,但它不起作用,我不知道为什么。
我阅读了 ActiveStorage 文档并尝试应用我在 Stackoverflow 上的两个不同答案中找到的解决方案,但无济于事,我可以
我收到以下错误:
> Clearing Database...
> Creating users...
rails aborted!
ActiveStorage::IntegrityError: ActiveStorage::IntegrityError
/mnt/c/Users/pedro/code/ruby-projects/tinder-clone/db/seeds.rb:20:in `block in <main>'
/mnt/c/Users/pedro/code/ruby-projects/tinder-clone/db/seeds.rb:12:in `times'
/mnt/c/Users/pedro/code/ruby-projects/tinder-clone/db/seeds.rb:12:in `<main>'
/mnt/c/Users/pedro/code/ruby-projects/tinder-clone/bin/rails:9:in `<top (required)>'
/mnt/c/Users/pedro/code/ruby-projects/tinder-clone/bin/spring:15:in `<top (required)>'
./bin/rails:3:in `load'
./bin/rails:3:in `<main>'
Tasks: TOP => db:seed
这是我的种子文件:
require 'faker'
require "open-uri"
puts "> Clearing Database..."
Account.destroy_all
puts "> Creating users..."
FILE = File.open('app/assets/images/portrait.jpg')
15.times do
account = Account.create(
first_name: Faker::Name.first_name,
last_name: Faker::Name.last_name,
username: Faker::Games::Witcher.monster,
email: Faker::Internet.email,
password: "123456",
)
account.images.attach(io: FILE, filename: 'person.jpg', content_type: 'image/jpg')
end
puts "> Done!"
这是我的模型:
class Account < ApplicationRecord
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :validatable
has_many_attached :images
end
感谢您的帮助,谢谢!
【问题讨论】:
标签: ruby-on-rails ruby rails-activestorage