2012-08-08 20 views
11

Tôi sử dụng Quản trị viên hoạt động và tôi cần tải lên Phòng trưng bày với nhiều hình ảnh. Tôi làm nó như thế nào? Mã của tôi:Hoạt động quản trị nhiều tệp/hình ảnh tải lên với kẹp giấy

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 

Và tôi có lỗi này:

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

Dòng nào ném lỗi? – Agis

+0

Tôi có cùng một vấn đề. Bạn đã giải quyết được vấn đề chưa? – Daniel

Trả lời

6

Hãy thử điều này:

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

Tôi nhận được 'phương thức không xác định' new_record? ' cho nil: NilClass' cho việc này. Có vẻ là 'has_many' có lỗi. –

+0

Phải, tôi đã sửa nó – Agis

+4

Thực ra, sử dụng 'accept_nested_attributes_for' trong mô hình (được chỉ ra bởi OP) đã sửa nó cho tôi. Lỗi của tôi! –

0

Được rồi, tôi quản lý để giải quyết nó:

Cố gắng làm như sau:

ActiveAdmin.register Gallery do 
    form html: { multipart: true } do |f| 
    f.inputs do 
     f.input :name 
     file_field_tag("gallery_images_url", multiple: true, name: "gallery[gallery_images_attributes][][url]") 
    end    
    f.buttons 
    end 
end 

Tôi đã nhận được giải pháp đó bằng cách theo dõi bài đăng trên blog này: http://www.tkalin.com/blog_posts/multiple-file-upload-with-rails-3-2-paperclip-html5-and-no-javascript