Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Active Storage raises ActiveSupport::MessageVerifier::InvalidSignature

To import an image file into the Rails app using Active Storage, I wrote a Rake like this:

task :import_file => :environment do
  path = Rails.root.join("tmp", "sample.jpg")
  data = File.read(path)

  post = Post.first
  post.image.attach(data)
end

When I executed this task, I got an Exception ActiveSupport::MessageVerifier::InvalidSignature.

How can I avoid this error?

The source code of Post model is:

class Post < ApplicationRecord
  has_one_attached :image
end

I use the default config/storage.yml.

test:
  service: Disk
  root: <%= Rails.root.join("tmp/storage") %>

local:
  service: Disk
  root: <%= Rails.root.join("storage") %>

The version of Rails is 5.2.0.beta2.

like image 516
Tsutomu Avatar asked Jan 09 '18 06:01

Tsutomu


2 Answers

On the Edge API document, I found the answer.

desc "Import file"
task :import_file => :environment do
  path = Rails.root.join("tmp", "sample.jpg")

  post = Post.first
  File.open(path) do |io|
    post.image.attach(io: io, filename: "sample.jpg")
  end
end
like image 95
Tsutomu Avatar answered Nov 28 '22 18:11

Tsutomu


Just leaving my answer here, just in case anyone encounters the same problem as me.

I made the silly mistake of not passing the resize key in the argument when creating a variant:

image_tag user.profile_photo.variant('200x200')

Should have passed:

image_tag user.profile_photo.variant(resize: '200x200')
like image 29
mridula Avatar answered Nov 28 '22 19:11

mridula