2

我已经苦苦挣扎了一个星期,我正在尝试在 active_admin 中创建一个表单,用户可以在其中选择几张图片,添加描述和标题,然后提交他的表单以创建看起来像画廊的东西

到目前为止,我已经使用命令创建了两个模型:

rails g model Gallery title:string description:text
rails g model Image url:text #just in case the user has LOTS of images to upload

以下是我的模型现在的外观:

画廊.rb

class Gallery < ApplicationRecord
  has_many :images
  accepts_nested_attributes_for :images, allow_destroy: true
end

图片.rb

class Image < ApplicationRecord

  belongs_to :gallery
  mount_uploader :image, ImageUploader #Using Carrier Wave
end

管理员/gallery.rb

  permit_params :title, :description, :images

  form html: { multipart: true }  do |f|
    f.inputs  do
      f.input :title
      f.input :description
      f.input :images, as: :file, input_html: { multiple: true }
    end
    f.actions
  end

我的问题是,即使我的“图像”表单出现,我也无法通过其他模型保存图像,在我的“公共/上传”目录中没有上传任何内容,也没有在我的数据库中写入任何内容。

我找不到任何有趣的互联网可以解决这个问题

随意要求另一个文件

欢迎任何帮助

4

1 回答 1

3

permit_params :title, :description, :images

为什么:images,我认为您的意思是images_attributes: [:url]

但这也行不通。我按照这里的步骤操作:https ://github.com/carrierwaveuploader/carrierwave/issues/1653#issuecomment-121248254

你可以只用一个模型

rails g model Gallery title:string description:text url:string

模型/画廊.rb

# your url is accepted as an array, that way you can attach many urls
serialize :url, Array
mount_uploaders :url, ImageUploader

注意:使用 Sqlite 的序列化,对于 Postgres 或其他一些能够处理数组的数据库读取:在 Rails 中添加一个数组列

管理员/gallery.rb

permit_params :title, :description, url: []
form html: { multipart: true }  do |f|
  f.inputs  do
    f.input :title
    f.input :description
    f.input :url, as: :file, input_html: { multiple: true }
  end
  f.actions
end
于 2017-07-15T19:13:37.640 回答