Ruby on rails 活动管理员/传送者保存多个图像上载
我已经挣扎了一个星期了,我正在尝试在active_admin中创建一个表单,用户可以在其中选择几张图片,添加描述和标题,然后提交表单以创建类似于图库的内容 到目前为止,我使用命令创建了两个模型:Ruby on rails 活动管理员/传送者保存多个图像上载,ruby-on-rails,model,activeadmin,multi-upload,Ruby On Rails,Model,Activeadmin,Multi 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 以下是我的模特们现在的样子: gal
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
class Image < ApplicationRecord
belongs_to :gallery
mount_uploader :image, ImageUploader #Using Carrier Wave
end
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
我的问题是,即使我的“图像”表单出现,我也无法通过另一个模型保存图像,在我的“public/upload”目录中没有上传任何内容,在我的数据库中也没有写入任何内容
我在互联网上找不到任何有趣的东西可以解决这个问题
请随意要求另一个文件
欢迎任何帮助
许可参数:标题、描述、图像
为什么:images,我想你是指images\u属性:[:url]
但这也行不通。我遵循以下步骤:
你只需要一个模型就可以做到
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
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
注意:对于Postgres或其他能够处理数组读取的数据库,请将serialize与Sqlite一起使用:
管理员/画廊.rb
# your url is accepted as an array, that way you can attach many urls
serialize :url, Array
mount_uploaders :url, ImageUploader
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
您可以通过更改
permit\u params
call(admin/gallery.rb)来解决问题:permit\u params:title、:description、images\u attributes:[:image]
Hope,这很有帮助