[英]Active admin / Carrierwave multiple image upload
我已经挣扎了一个星期,我正在尝试在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
管理员/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: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
型号/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或其他能够处理数组的数据库一起使用,请阅读: 在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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.