【问题标题】:Active admin / Carrierwave multiple image uploadActive admin / Carrierwave 多张图片上传
【发布时间】:2016-09-18 14:06:26
【问题描述】:

我已经苦苦挣扎了一周,我正在尝试在 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

这是我的模型现在的样子:

gallery.rb

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

image.rb

class Image < ApplicationRecord

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

admin/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

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

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

请随时索取其他文件

欢迎任何帮助

【问题讨论】:

  • 您可以通过更改permit_params来解决您的问题调用(admin/gallery.rb):permit_params :title, :description, images_attributes: [:image]希望,它会有所帮助

标签: ruby-on-rails model activeadmin multi-upload


【解决方案1】:

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

models/gallery.rb

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

注意:对 Sqlite 使用序列化,对于 Postgres 或其他一些能够处理数组的数据库,请阅读:Add an array column in Rails

admin/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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-03-02
    • 2015-05-17
    • 1970-01-01
    • 2022-01-25
    • 1970-01-01
    • 1970-01-01
    • 2018-02-12
    相关资源
    最近更新 更多