Rails,活动存储,尝试附加照片时出现错误消息
Rails, active storage, Error message when I try to attach a photo
我正在尝试将照片直接附加到 seed.rb
中的实例。但我不认为这是问题所在,因为它在控制台中也不起作用。
当我 运行 rails db:seed
我收到此错误消息:
Errno::ENOENT: No such file or directory @ rb_sysopen - ../app/assets/images/image.jpg
这是我的模型:
class Restaurant < ApplicationRecord
restaurant_types = ["chinese", "italian", "japanese", "french", "belgian"]
has_one_attached :photo
has_many :reviews, dependent: :destroy
validates :name, :address, :category, :phone_number, presence: true
validates :category, inclusion: { in: restaurant_types, message: "%{value} is not a valid size" }
end
这是我的种子文件:
require 'open-uri'
require "faker"
puts "deleting all restaurants and reviews"
Review.delete_all
Restaurant.delete_all
puts Restaurant.count
puts Review.count
puts "creating new restaurants"
20.times do
name = Faker::Restaurant.name
address = Faker::Address.community
phone = Faker::PhoneNumber.cell_phone
category = ["chinese", "italian", "japanese", "french", "belgian"].sample
restaurant = Restaurant.create(name: name, address: address, phone_number: phone, category: category)
restaurant.photo.attach(io: File.open('../app/assets/images/image.jpg'), filename: 'image')
end
而且我还在我的 config/application.rb
文件中包含了 require "active_storage/engine"
。
这是我的应用程序文件夹以及我的 seed.rb 和 image.jpg 文件所在的位置。显然 link?
有问题
有人知道我做错了什么吗?
谢谢!
您需要通过Rails.root
获取您的应用程序的路径
在你的种子文件中更改为这个并尝试
restaurant.photo.attach(io: File.open(Rails.root.join("app", "assets", "images", "image.jpg")), filename: 'image.jpg')
您也可以将其存储在变量中
file_path = Rails.root.join("app", "assets", "images", "image.jpg")
restaurant.photo.attach(io: File.open(filepath), filename: 'image.jpg')
此外,添加文件存在的条件
File.exist? file_path
我正在尝试将照片直接附加到 seed.rb
中的实例。但我不认为这是问题所在,因为它在控制台中也不起作用。
当我 运行 rails db:seed
我收到此错误消息:
Errno::ENOENT: No such file or directory @ rb_sysopen - ../app/assets/images/image.jpg
这是我的模型:
class Restaurant < ApplicationRecord
restaurant_types = ["chinese", "italian", "japanese", "french", "belgian"]
has_one_attached :photo
has_many :reviews, dependent: :destroy
validates :name, :address, :category, :phone_number, presence: true
validates :category, inclusion: { in: restaurant_types, message: "%{value} is not a valid size" }
end
这是我的种子文件:
require 'open-uri'
require "faker"
puts "deleting all restaurants and reviews"
Review.delete_all
Restaurant.delete_all
puts Restaurant.count
puts Review.count
puts "creating new restaurants"
20.times do
name = Faker::Restaurant.name
address = Faker::Address.community
phone = Faker::PhoneNumber.cell_phone
category = ["chinese", "italian", "japanese", "french", "belgian"].sample
restaurant = Restaurant.create(name: name, address: address, phone_number: phone, category: category)
restaurant.photo.attach(io: File.open('../app/assets/images/image.jpg'), filename: 'image')
end
而且我还在我的 config/application.rb
文件中包含了 require "active_storage/engine"
。
这是我的应用程序文件夹以及我的 seed.rb 和 image.jpg 文件所在的位置。显然 link?
有问题有人知道我做错了什么吗? 谢谢!
您需要通过Rails.root
在你的种子文件中更改为这个并尝试
restaurant.photo.attach(io: File.open(Rails.root.join("app", "assets", "images", "image.jpg")), filename: 'image.jpg')
您也可以将其存储在变量中
file_path = Rails.root.join("app", "assets", "images", "image.jpg")
restaurant.photo.attach(io: File.open(filepath), filename: 'image.jpg')
此外,添加文件存在的条件
File.exist? file_path