Rails,使用 carrierwave 上传多张图片,但 json 我的数据库不支持

Rails, uploading multiple images with carrierwave, but json not supported by my database

我正在尝试将 Carrierwave 用于多张图片上传。 在遵循 github 上的载波指南后,我做了:

rails g migration add_images_to_areas images:json
rake db:migrate

但是在我的模式中,我的区域 table 没有显示,相反我得到:

# Could not dump table "areas" because of following StandardError
#   Unknown type 'json' for column 'images'

我现在该怎么办?我应该使用其他类型而不是 json,但是什么?

抱歉,如果这是一个业余问题。

数据库默认不支持数组、散列等。

为此,您可以将此代码添加到模型中对其进行序列化:

class Name_class < ActiveRecord::Base
  serialize :column_name, JSON
end

并更改迁移字段:

add_column :user_preferences, :text

这会将信息作为 Text 插入数据库,当您检索它时,它将是 JSON

有关 serialization 的更多信息,请点击此处 RailsGuides#Serialize

我不确定这里出了什么问题,Carrierwave 的 github 教程建议 :json,但你不知道必须这样做。

在我按照此处使用 Carrierwave 上传多张图片的指南后一切顺利:Rails 4 multiple image or file upload using carrierwave