6

为了使用 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

当我执行这个任务时,我得到了一个 Exception ActiveSupport::MessageVerifier::InvalidSignature

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

模型源代码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。

4

2 回答 2

8

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
于 2018-01-09T07:28:29.873 回答
4

把我的答案留在这里,以防万一有人遇到和我一样的问题。

resize我犯了一个愚蠢的错误,即在创建时没有在参数中传递密钥variant

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

应该通过:

image_tag user.profile_photo.variant(resize: '200x200')
于 2019-01-15T17:57:06.833 回答