Software engineer, data guy, Open Source enthusiast, New Hampshire resident, husband, father. Fan of guitars, hiking, photography, homebrewing, sarcasm.
Importing and converting CSV data into a Rails model with a slick Active Admin interface
I recently exported a few data sources to CSV and thought it would interesting to migrate the data into a Ruby on Rails model and use Active Admin to create a slick admin interface. Here is the skeletal code I used.
Part 1, Rails setup
Part 2, Model creation
I added acts as taggable on gem, for tagging (optional). edit file: Gemfile
Execute bundle to install gems
Create Post model
Add taggable property to Post model & make mass-assignable. file: app/models/post.rb
Execute acts as taggable migration
Setup database
Part 3, Active Admin integration
Add active admin gem; edit file: Gemfile
Execute bundle to install gems
Run active admin generator, and setup database. Register Post model with active admin
Part 4, Active Admin + acts-as-taggable-on integration
In this section I (optionally) show how you might alter the active admin post file to integrate with acts-as-taggable-on for tagging. Edit file: app/admin/post.rb:
Add root route to active admin dashboard. edit file: config/routes.rb
Remove public facing index.html file
Update default/admin user, or create a new user via console
Part 5, Rake task to import CSV data
Create a rake task to import the CSV data. new file: lib/tasks/import_csv_data.rake
Execute the rake task to import the CSV data into the Rails model
Start the WEBrick server (development mode)
Here is a screenshot of the CSV data file I imported, and below, the active admin interface that was created: