0

我不会在 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 admin 我的 rails 版本 3.2.13 ruby​​ 1.9.3 上传多个文件

4

1 回答 1

3

我写了一篇关于使用 RailsAdmin 和 HTML5 上传多个文件的教程,也许你会发现它很有帮助:http ://blog.powpark.com/2013/10/16/multiple-file-upload-with-html5-and-rails-行政/


基本上你需要创建一个自定义 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'

我希望这是有帮助的。

于 2013-10-16T10:40:19.550 回答