簡體   English   中英

將圖像添加到Rails 5.2活動存儲中的種子數據庫

[英]Adding image to seed database in rails 5.2 active storage

只是嘗試將圖像添加到我的種子文件。 嘗試在Rails 5.2中使用新的活動存儲進行添加時遇到一些問題

任何人都可以發現正在發生的事情。 這是代碼,后跟db中的錯誤。 謝謝。

25.times do
temp = Post.create([{
    title: Faker::Book.unique.title,
    content: Faker::Lorem.paragraphs(rand(100..200)).join('\n'),
    category_id: rand(1..5),
    user_id: 1,
    status: 1,
    recommended: [true, false].sample,
    excerpt: Faker::Lorem.paragraph(10) 
        }])
  puts Post.first.featured_image
  temp.first.featured_image.attach(io: File.open('/Users/bradley/'), filename: 'Dart.png', content_type: 'image/png')

結束

錯誤:

#<ActiveStorage::Attached::One:0x00007fb672434448>
rake aborted!
Errno::EISDIR: Is a directory @ io_fread - /Users/bradley/

您在此代碼中傳遞目錄而不是文件:

File.open('/Users/bradley/')

如果您的圖像路徑是: /Users/bradley/Dart.png 然后,您需要更改代碼:

File.open('/Users/bradley/Dart.png')

但是,即使只用於種子,我也不建議在項目中使用絕對路徑。 您可以將圖像添加到Rails項目中,並使用項目的相對路徑。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM