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

问题描述 投票:6回答:2

我正在尝试在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找不到任何相关信息

我有迁移活动存储,我通过了确定

ruby-on-rails ruby ruby-on-rails-5 rails-activestorage ruby-on-rails-5.2
2个回答
11
投票

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

has_one_attached :image

在没有创建Dishescolumn的imagemodel内。


4
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.