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'
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')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With