无法在 Rails 中使用 ActiveStorage 创建文件字段

Can't create file field with ActiveStorage in Rails

我正在尝试在 Rails 5.2 中使用 Active Storage。我发现我应该在迁移中创建类型为 file 的字段,但是我有一个错误:

$ rdm
Running via Spring preloader in process 40193
== 20171217191942 CreateDishes: migrating 
=====================================
-- create_table(:dishes)
rake aborted!
StandardError: An error has occurred, this and all later migrations canceled:

undefined method `file' for #<ActiveRecord::ConnectionAdapters::PostgreSQL::TableDefinition:0x00007fd56e297750>
/Users/alder/Projects/_apps/service_exchange/rails-backend/db/migrate/20171217191942_create_dishes.rb:6:in `block in change'
/Users/alder/Projects/_apps/service_exchange/rails-backend/db/migrate/20171217191942_create_dishes.rb:3:in `change'
-e:1:in `<main>'

迁移:

class CreateDishes < ActiveRecord::Migration[5.2]
  def change
    create_table :dishes do |t|
      t.string :name, index: true
      t.string :description
      t.file :image

      t.timestamps
    end
  end
end

我试图创建字符串字段,但它不起作用。

official docs

中找不到任何相关信息

我有活动存储的迁移,我通过了 ok

你可以检查这个问题 (ActiveRecord field type) 因为没有像 file 这样的任何类型如果你需要上传一个你可以用类型 string 创建的文件比如 t.string

主动存储使用 setuprails active_storage:install 两个表,而不是您需要在自己的迁移中创建的专用字段 (t.file :image)。 当您设置 storage.yml 时,您应该可以使用

has_one_attached :image

Dishes 模型中而不创建 image 列。