使用回形针主动管理多个文件/图像上传

qua*_*ain 12 ruby-on-rails paperclip formtastic activeadmin

我使用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
Run Code Online (Sandbox Code Playgroud)

我有这个错误:

Image(#70319146544460) expected, got ActionDispatch::Http::UploadedFile(#70319105893880)
Run Code Online (Sandbox Code Playgroud)

Agi*_*gis 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
Run Code Online (Sandbox Code Playgroud)

  • 实际上,在模型中使用`accepts_nested_attributes_for`(由OP指出)为我修复了它.我的错! (4认同)
  • 我得到`undefined方法`new_record?' 为nil:NilClass`为此.好像是`has_many`. (2认同)
  • 我们能够一次上传多个文件吗? (2认同)