[英]Active admin multiple image upload with paperclip Rails 5
I use Active admin and I need upload photos for my project. 我使用的是Active admin,并且需要为我的项目上传照片。 How can I do it?
我该怎么做? My code:
我的代码:
class Project < ApplicationRecord
has_many :images
accepts_nested_attributes_for :images
end
class Image < ApplicationRecord
belongs_to :project
has_attached_file :image
validates_attachment_presence :image
end
ActiveAdmin.register Project do
permit_params :project_name , :project_location , :project_status , :project_area , :project_prices , :project_info , :project_description , :image , :image_file_name, :image_content_type, :image_file_size, :image_updated_at
index do
column :project_name
column :project_description
actions
end
form :html => { :enctype => "multipart/form-data" } do |f|
f.inputs 'Project Info' do
f.input :project_name
f.input :project_description
end
f.inputs do
f.has_many :images do |p|
p.input :image , as: :file
end
end
f.actions
end
end
With this code i can create Project without image. 使用此代码,我可以创建没有图像的项目。 But i can't add any image to db.
但我不能将任何图像添加到数据库。 Waiting for help !!!
等待帮助!
I solve the problem ! 我解决了问题! working code :
工作代码:
permit_params :project_name , :project_location , :project_status , :project_area , :project_prices , :project_info , :project_description , images_attributes: [:image , :id , :_destroy]
f.inputs do
f.has_many :images , heading: false, allow_destroy: true do |ff|
ff.input :image, required: true, as: :file
end
end
most important part that i miss is : images_attributes: [:image , :id , :_destroy]
if you don't write this part fully , it won't work ! 我想念的最重要的部分是:
images_attributes: [:image , :id , :_destroy]
如果您没有完全写完这一部分,它将无法正常工作!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.