Can't create file field with ActiveStorage in Rails

时间:2017-12-18 04:52:51

标签: ruby-on-rails ruby ruby-on-rails-5 rails-activestorage ruby-on-rails-5.2

I'm trying to use Active Storage in Rails 5.2. I found that I should create field with type file in migration, but I have an error:

$ 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>'

Migration:

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

I was trying to create string field, but it doesn't work.

Couldn't find anything about that in official docs

I have migration for active storage and I passed ok

2 个答案:

答案 0 :(得分:11)

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

has_one_attached :image

Dishes模型中,但未创建image列。

答案 1 :(得分:4)

您可以查看此问题(ActiveRecord field type),因为如果您需要上传可以使用类型file创建的文件string t.string类型的文件p>