active_admin and adding multiple images to gallery

非 Y 不嫁゛ 提交于 2019-12-07 19:28:09

问题


I'm using active_admin and carrierwave gems. Have two simple models:

class Image < ActiveRecord::Base
  attr_accessible :gallery_id, :file
  belongs_to :gallery

  mount_uploader :file, FileUploader
end

class Gallery < ActiveRecord::Base

  attr_accessible :description, :title, :file, :images_attributes
  has_many :images
  accepts_nested_attributes_for :images, allow_destroy: true

  mount_uploader :file, FileUploader
end

Now my active_admin form for Gallery looks like this:

form do |f|
  f.inputs "Gallery" do
    f.input :title
  end
  f.has_many :images do |ff|
    ff.input :file
  end
  f.actions
end

Now I can upload one file, click "Add New Image" and upload another one. Instead of it, I'd like to click "Add new Image", select multiple files and upload them all at once. Any idea how can I implement it?


回答1:


For a Gallery form with multiple image uploads you can try this

admin/galleries.rb

  form do |f|
    f.inputs "Gallery" do 
      f.input :name
    end
    f.has_many :images do |ff|
      ff.input :file
    end
  end

In model/gallery.rb:

attr_accessible :images_attributes

In model/gallery.rb (add after relations):

accepts_nested_attributes_for :images, :allow_destroy => true


来源:https://stackoverflow.com/questions/16625437/active-admin-and-adding-multiple-images-to-gallery

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!