Active Storage 引发 ActiveSupport::MessageVerifier::InvalidSignature [英] Active Storage raises ActiveSupport::MessageVerifier::InvalidSignature

查看:59
本文介绍了Active Storage 引发 ActiveSupport::MessageVerifier::InvalidSignature的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了使用 Active Storage 将图像文件导入到 Rails 应用程序中,我写了一个这样的 Rake:

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

当我执行这个任务时,我得到一个异常ActiveSupport::MessageVerifier::InvalidSignature.

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

我怎样才能避免这个错误?

How can I avoid this error?

Post模型的源码为:

class Post < ApplicationRecord
  has_one_attached :image
end

我使用默认的config/storage.yml.

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

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

Rails 的版本是 5.2.0.beta2.

The version of Rails is 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

这篇关于Active Storage 引发 ActiveSupport::MessageVerifier::InvalidSignature的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆