使用Paperclip实现Active Admin多文件/图片上传

12

我使用 Active Admin,并且需要上传包含大量图像的图库。我该如何做?

我的代码:

class Gallery < ActiveRecord::Base
  belongs_to :event
  has_many :images

  attr_accessible :name, :publish, :images, :image, :images_attributes
  accepts_nested_attributes_for :images, allow_destroy: true

  validates :name, presence: true

end

class Image < ActiveRecord::Base
  belongs_to :gallery

  attr_accessible :url
  has_attached_file :url, :styles => { :medium => "300x300>", :thumb => "100x100>" }
end


ActiveAdmin.register Gallery do
    form html: { multipart: true }  do |f|
          f.inputs  do
            f.input :name
            f.input :images, as: :file, input_html: { multiple: true}
          end            
          f.buttons
    end  
end

我遇到了这个错误:

Image(#70319146544460) expected, got ActionDispatch::Http::UploadedFile(#70319105893880)

我有同样的问题。你解决了这个问题吗? - Daniel
2个回答

6

试试这个:

ActiveAdmin.register Gallery do
  form multipart: true do |f|
    f.inputs do
      f.input :name

      f.has_many :images do |p|
        p.input :url
      end
    end

    f.actions
  end
end

2
我得到了 undefined method new_record?' for nil:NilClass的错误。看起来是has_many` 出了问题。 - Mild Fuzz
4
实际上,使用模型中的accepts_nested_attributes_for(由原帖作者指出)解决了我的问题。我的错! - Mild Fuzz
2
我们能否使用这个功能一次上传多个文件? - Sravan

0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接