Active Storage raises ActiveSupport::MessageVerifier::InvalidSignature

感情迁移 提交于 2020-01-13 10:43:13

问题


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.


回答1:


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



回答2:


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


来源:https://stackoverflow.com/questions/48162667/active-storage-raises-activesupportmessageverifierinvalidsignature

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!