gemのrefileを使った画像投稿機能でseedファイルに画像データを事前登録したいのですがうまくいきません。
コードは以下のように書きました。
names = %w(Taro Jiro Harry Ron Mike) 0.upto(4) do |idx| user = User.new( name: names[idx], email: "#{names[idx]}@example.com", password: "000000", password_confirmation: "000000" # profile_image_id: idx + 1, # profile_image: Rails.root.join("app/public/images/sample-author2.jpg").open ) File.open("#{Rails.root}/app/public/images/sample-author2.jpg", "rb") do |file| user.profile_image = file end user.save! end end
Userテーブルのprofile-imageカラムにデータが保存されません。
File.open("#{Rails.root}/app/public/images/sample-author2.jpg", "rb") do |file| user.profile_image = file end
をコメントアウトすれば他のデータはちゃんと保存されます。
画像データを事前に保存するにはどのように書けばいいでしょうか?
ご教示願います。
Rails 5.2.1
Ruby 2.3.7
あなたの回答
tips
プレビュー