stevec
stevec

Reputation: 52198

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'

Upvotes: 17

Views: 12858

Answers (1)

stevec
stevec

Reputation: 52198

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')

Upvotes: 25

Related Questions