我正在尝试在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找不到任何相关信息
我有迁移活动存储,我通过了确定
您需要在自己的迁移(t.file :image
)中创建专用字段,而不是使用setup使用rails active_storage:install
的两个表。当您设置storage.yml时,您应该可以使用
has_one_attached :image
在没有创建Dishes
column的image
model内。
你可以查看这个问题(ActiveRecord field type),因为你没有像file
那样的任何类型如果你需要上传一个你可以用类型string
创建的文件,如t.string