[英]How to use storage to upload a file without active record in Rails 5?
[英]Use create an record with Active Storage without ActionParameters
我嘗試使用圖像,directyl和create以及模型創建記錄
Banner.create(start_at:Time.now,location:'site',image_fr:File.new(“#{Rails.root} /db/seeds/images/banners/pdfs/gift_voucher_bottom.png”,'r'), image_de:File.new(“#{Rails.root} /db/seeds/images/banners/pdfs/gift_voucher_bottom.png”,'r'),寬度:595,高度:nil)
由於驗證,我必須在創建時執行此操作,而不是在之后執行.attach。 我們可以做到嗎?
您可以傳遞一個代碼塊來create
。 在保存之前,它將與新記錄一起調用:
Banner.create(start_at: Time.now, location: 'site', width: 595, height: nil) do |banner|
Rails.root.join('db/seeds/images/banners/pdfs/gift_voucher_bottom.png').open('rb') do |io|
banner.image_fr.attach(io: io, filename: 'banner.png', content_type: 'image/png')
end
# ...
end
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.