2013-10-01 80 views
0

我不會上傳多個文件中rails_admin無法上傳多個文件中rails_admin

我的模型配置

class EducationMaster < ActiveRecord::Base 
    attr_accessible :address, :city, :country, :name, :state, :zip,:photos 
    has_many :photos, :dependent => :destroy 
end 

class Photo < ActiveRecord::Base 
    belongs_to :education_master 
    attr_accessible :education_master_id, :image 
    mount_uploader :image, ImageUploader 
end 

請幫我使用Rails上傳多個文件管理 我的rails 3.2.13版本紅寶石1.9.3

回答

3

我寫了一個關於與RailsAdmin和HTML5多文件上傳教程,也許你會發現它的幫助:http://blog.powpark.com/2013/10/16/multiple-file-upload-with-html5-and-rails-admin/


基本上你需要創建一個自定義RailsAdmin操作,使用:

rails plugin new rails_admin_multiple_upload -m https://gist.github.com/bbenezech/1621146/raw/5268788e715397bf476c83d76d335f152095e659/rails_admin_action_creator --skip-gemfile --skip-bundle -T -O -S -J --full 

然後修改引擎文件:

class MultipleUpload < Base 
    RailsAdmin::Config::Actions.register(self) 
    register_instance_option :member do 
    true 
    end 

    register_instance_option :link_icon do 
    'icon-upload' 
    end 

    register_instance_option :http_methods do 
    [:get, :post] 
    end 

    register_instance_option :controller do 
    Proc.new do 
     @response = {} 

     if request.post? 
     @album = Album.find_by_id(params[:album_id]) 
     @album.update_attribute(:photos_attributes, params[:album][:photos_attributes]) 
     end 

     render :action => @action.template_name 
    end 
    end 
end 

修改視圖文件:

= simple_form_for(rails_admin.multiple_upload_url(@abstract_model.to_param), html: { multipart: true }) do |f| 
    = f.input :album_id, :as => :hidden, :input_html => { :name => "album_id", :value => @object.id } 
    = file_field_tag('album_photos_file', multiple: true, name: "album[photos_attributes][][file]") 
    = f.submit :submit, value: 'Upload', name: 'Upload', :class => 'btn btn-primary' 

我希望這是有幫助的。