无法在rails_admin中上传多个文件

时间:2013-10-01 10:42:58

标签: ruby-on-rails ruby-on-rails-3 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 admin上传多个文件 我的rails版本3.2.13 ruby​​ 1.9.3

1 个答案:

答案 0 :(得分: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'

我希望这有用。