Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to attach an Active Storage image from local hard disk from the rails console?

I am trying to learn how to attach an image from local hard disk to Active Storage. E.g.

User.last.images.attach("../../Downloads/me.jpg")

But I see

ActiveSupport::MessageVerifier::InvalidSignature: ActiveSupport::MessageVerifier::InvalidSignature
from /Users/st/.rbenv/versions/2.7.1/lib/ruby/gems/2.7.0/gems/activesupport-6.0.3.2/lib/active_support/message_verifier.rb:176:in `verify'
like image 265
stevec Avatar asked Dec 06 '25 09:12

stevec


1 Answers

The problem was you shouldn't just provide the file path, but instead give the file path to File.open() (the error message doesn't necessarily make that obvious).

Once that's out of the way, a filename must also be provided, so the full answer is:

User.last.images.attach(io: File.open("../../Downloads/me.jpg"), filename: "something")

Note: another user provided a more general approach:

User.last.images.attach(io: File.open("#{Rails.root}/app/assets/images/my_image.png"), filename: 'my_image.png', content_type: 'image/png')
like image 56
stevec Avatar answered Dec 08 '25 23:12

stevec