This gem adds an import-action in every controller you like to be able to handle csv-Imports. It includes the complete workflow: Csv-Upload, auto-matching and review, importing data into model.
Rails 3 & Ruby 1.8.7
In principle it will also work with Ruby 1.9, whereas changes has to be made as FasterCSV is already integrated named as CSV.
Gemfile:
gem 'csv_mapper', :git => git://github.com/masche842/csv_mapper.git
Set up a new route to point to the import action (get & post will be needed!):
routes.rb
resources :myresource do get 'import', :on => :collection post 'import', :on => :collection end
Include it in your Controller:
require 'csv_mapper' include CsvMapper::ControllerActions
Set up the fields to map to:
csv_mapper_config( :mapping => { "Firstname" => :firstname, "Lastname" => :lastname } )
See also the implementation in spec/dummy!
call /myresources/import!
This gem is heavily based on Andrew Timberlake’s map-fields-gem (github.com/internuity/map-fields). Nevertheless I didn’t fork it, because the changes are fundamental.
MIT License. Copyright 2011 magiclabs* (magiclabs.de)