首页 > 解决方案 > Rails Active Storage Local attachemnt 因神秘错误而失败:未初始化的常量 Analyzable

问题描述

红宝石'2.7.1'/导轨6.0.3.2

我无法让本地活动存储附件工作。这是我的设置:

我跑了:

$ rails active_storage:install
$ rake db:migrate

模型架构

  create_table "amazon_uploads" do |t|
    t.string "status"
    t.string "order_csv"
    t.datetime "created_at", precision: 6, null: false
    t.datetime "updated_at", precision: 6, null: false
  end

模型:

class AmazonUpload < ApplicationRecord
  has_many :amazon_upload_recipients

  has_one_attached :order_csv
end

我的 storage.yml

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

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

我的 development.rb 包含:

  config.active_storage.service = :local

然后在我的代码中我这样做:

    file = File.open(updated_file_path)
    @amazon_upload.order_csv.attach(io: file, filename: "hello", content_type: "application/CSV")

最后一行失败了

NameError: uninitialized constant #<Class:0x0000564c2f7e8638>::Analyzable

我可以看到这个错误的唯一其他参考,该人声称他们有一个类型,但我认为这不是问题所在

标签: ruby-on-railsruby-on-rails-6rails-activestorage

解决方案


这是 zeitwerk 错误,您只需将 rails 版本升级到 6.1.x,一切都应该正常

如果您不想升级 - 通过添加 application.rb 切换到经典加载模式

config.autoloader = :classic

推荐阅读