2012-08-08 57 views
11

我使用活动管理员,我需要上传带有大量图像的图库。我该怎么做? 我的代码:使用回形针主动管理多个文件/图像上传

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) 
+0

哪一行会引发错误? – Agis 2012-10-21 12:12:56

+0

我有同样的问题。你有没有解决这个问题呢? – Daniel 2013-03-25 12:02:52

回答

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

我得到'未定义的方法'new_record?'为零:NilClass'为此。似乎是'has_many'的错。 – 2012-10-22 20:13:21

+0

对,我修正了它 – Agis 2012-10-22 22:03:27

+4

实际上,在模型中使用'accep_nested_attributes_for'(由OP指出)为我修复了它。我的错! – 2012-10-23 08:04:53

相关问题