2017-08-19 11 views
0

私はRoRの初心者で、インポートデータ(csv)のフォレスト管理にカスタムアクションを実装したいと考えています。フォレスト管理:インポートデータカスタムアクションを作成しようとすると「無効なデータURI:nil」

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 /コレクション/ 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 

そして私 http://doc.forestadmin.com/developers-guide/#triggering-an-action-from-the-collection

私は私のactions_controller(コントローラ/森/ actions_controller.rb)を持っています私のルートに追加:

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' 

答えて

2

設定では、このフィールドの名前はimporter csvであるため、このフィールド名は要求で送信されます。

のlib/forest_liana /コレクション/ 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 

あなたはこのようなあなたのアクションに名前を付けたい場合は、ここで適切な構成であります

0

迅速な提案書き込みrequire 'data_uri'

関連する問題