主动管理多图像上传与回形针Rails 5



我使用Active admin,我需要为我的项目上传照片。我该怎么做呢?我的代码:

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
使用这段代码,我可以创建没有图像的项目。但是我不能添加任何图像到数据库。等待救援!!

我解决了这个问题!工作代码:

 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

我错过的最重要的部分是:images_attributes: [:image , :id , :_destroy]如果你不完整地写这部分,它将无法工作!

最新更新