Tsu*_*omu 5 ruby-on-rails rails-activestorage
为了使用Active Storage将图像文件导入Rails应用程序,我编写了一个Rake,如下所示:
task :import_file => :environment do
path = Rails.root.join("tmp", "sample.jpg")
data = File.read(path)
post = Post.first
post.image.attach(data)
end
Run Code Online (Sandbox Code Playgroud)
执行此任务时,出现Exception ActiveSupport::MessageVerifier::InvalidSignature
。
如何避免此错误?
该Post
模型的源代码为:
class Post < ApplicationRecord
has_one_attached :image
end
Run Code Online (Sandbox Code Playgroud)
我使用默认值config/storage.yml
。
test:
service: Disk
root: <%= Rails.root.join("tmp/storage") %>
local:
service: Disk
root: <%= Rails.root.join("storage") %>
Run Code Online (Sandbox Code Playgroud)
Rails的版本是5.2.0.beta2。
在Edge API文档中,找到了答案。
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
Run Code Online (Sandbox Code Playgroud)
只是将我的答案留在这里,以防万一有人遇到与我相同的问题。
我犯了一个愚蠢的错误,resize
在创建时没有传递参数中的密钥variant
:
image_tag user.profile_photo.variant('200x200')
Run Code Online (Sandbox Code Playgroud)
应该已经通过:
image_tag user.profile_photo.variant(resize: '200x200')
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
3103 次 |
最近记录: |