0

我是 RoR 的初学者,我想在森林管理员中为导入数据 (csv) 实施自定义操作。 http://doc.forestadmin.com/developers-guide/#triggering-an-action-from-the-collection

我有我的 actions_controller (controllers/forest/actions_controller.rb):

class Forest::ActionsController < ForestLiana::ApplicationController
  require 'data_uri'
  def bulk_import
    uri = URI::Data.new(params.dig('data', 'attributes', 'values', 'file'))
    uri.data.force_encoding('utf-8')
    CSV.parse(uri.data).each do |row|
      Geo.create!({
        :departement=> row[0],
        :slug => row[1],
        :nom => row[2],
        :nom_simple => row[3],
     })
    end
    render json: { success: 'Data successfuly imported!' }
  end
end

我的收藏(lib/forest_liana/collections/geo.rb):

class Forest::Geo
  include ForestLiana::Collection
  collection :geos
  action 'bulk_import', global: true,
  fields: [{
    field: 'importer csv', type: 'File', isRequired: true, description: "Personal description",
  }]
end

我添加了我的路线:

namespace :forest do
  post '/actions/bulk_import' => 'actions#bulk_import'
end

这不起作用...:/

消息错误:

Started OPTIONS "/forest/actions/bulk_import" for 127.0.0.1 at 2017-08-19 18:52:30 +0200
Started POST "/forest/actions/bulk_import" for 127.0.0.1 at 2017-08-19 18:52:30 +0200
Processing by Forest::ActionsController#bulk_import as HTML
  Parameters: {"data"=>{"attributes"=>{"ids"=>[], "values"=>{"importer csv"=>"data:text/csv;base64,77u/ZGVwYXJ0ZW1lbnQsIHNsdWcsIG5vbSwgbm9tX3NpbXBsZQ0iMSwiIjAxIiIsIiJvemFuIiIsIiJPWkFOIiIi"}, "collection_name"=>"geos"}, "type"=>"custom-action-requests"}}
Completed 500 Internal Server Error in 3ms (ActiveRecord: 0.0ms)



URI::InvalidURIError - Invalid Data URI: nil:
  data_uri (0.1.0) lib/data_uri/uri.rb:15:in `initialize'
  app/controllers/forest/actions_controller.rb:4:in `bulk_import'
4

2 回答 2

2

在您的配置中,该字段已命名importer csv,因此将在请求中发送此字段名称。

lib/forest_liana/collections/geo.rb

class Forest::Geo
  include ForestLiana::Collection
  collection :geos
  action 'bulk_import', global: true,
  fields: [{
    field: 'importer csv', type: 'File', isRequired: true, description: "Personal description",
  }]
end

在您的控制器中,您应该执行以下操作:

uri = URI::Data.new(params.dig('data', 'attributes', 'values', 'importer csv'))

代替 :

uri = URI::Data.new(params.dig('data', 'attributes', 'values', 'file'))

看起来importer csv应该是动作的名称而不是字段。

如果你想这样命名你的动作,这里是合适的配置:

class Forest::Geo
  include ForestLiana::Collection
  collection :geos
  action 'Importer CSV', global: true,
  fields: [{
    field: 'importer csv', type: 'File', isRequired: true, description: "Personal description",
  }]
end
于 2017-08-25T13:58:43.737 回答
0

快速建议写require 'data_uri'在类名上方。

于 2017-08-20T05:57:12.900 回答