问题
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