How to import CSV file in rails

July 18, 2007 at 6:17 pm 9 comments

How to import CSV file in rails?

The CSV text-file format is a common choice for both import and export when performing data migrations. What if you want to import CSV files within a Rails application?

Here is the code that allow you to upload CSV data onto the MySQL database from the web interface
This code save data direct to database without saving it to temp file

Controller:

    require 'csv'

   def csv_import 
     @parsed_file=CSV::Reader.parse(params[:dump][:file])
     n=0
     @parsed_file.each  do |row|
     c=CustomerInformation.new
     c.job_title=row[1]
     c.first_name=row[2]
     c.last_name=row[3]
     if c.save
        n=n+1
        GC.start if n%50==0
     end
     flash.now[:message]="CSV Import Successful,  #{n} new records added to data base"
   end

View:
your view will look like

 <% form_for :dump, :url=>{:controller=>"customer_informations", :action=>"csv_import"}, :html => { :multipart => true } do |f| -%>
 <table">
   <tr>
     <td>
      <label for="dump_file">
        Select a CSV File :
      </label>
     </td>
     <td >
       <%= f.file_field :file -%>
     </td>
   </tr>
   <tr>
     <td colspan='2'>
       <%= submit_tag 'Submit' -%>
     </td>
   </tr>
 </table>
<% end -%>

Entry filed under: CSV, Rails, ror, Ruby on Rails.

Varify Email address Format How to Paginate With Ajax

9 Comments Add your own

  • 1. Javier Alvarez  |  August 21, 2007 at 5:34 am

    Nice code!!!
    Do you know any way to use this with the FasterCSV class?

  • 2. suri  |  November 1, 2007 at 11:53 am

    Thanks for your webpage. It is useful for a project I am working on to be finished this weekend. However, the text of your web page is truncating at line ends on my browser IE6 and also in Firefox.

    Can you fix it or show me how to fix if problem is at my end.

    Surinder Jain

  • 3. Satish  |  November 2, 2007 at 10:21 am

    Everything is all right. If u are unable to read you can find same post on http://www.satishchauhan.com/articles/show_comments/5

  • 4. tdelquie  |  November 28, 2007 at 8:19 pm

    Quick and easy. Thanks a lot, it works really well.

  • 5. Ben  |  March 10, 2008 at 3:22 pm

    Wow, thank you! This was exactly what I was looking for. Thanks for the code, you saved me a ton of time! I really appreciate you sharing your good work with everyone!

  • 6. pedro  |  April 2, 2008 at 2:34 am

    Hi-

    I tried this code and I get a nil object when I load the page. Does this work in Rails 2.0?

  • 7. csvbadly  |  April 5, 2008 at 5:03 am

    erroy

    You have a nil object when you didn’t expect it!
    You might have expected an instance of ActiveRecord::Base.
    The error occurred while evaluating nil.[]

    ‘csv_import’

  • 8. GregH  |  April 24, 2008 at 1:50 am

    How do you getting end of line delineation to work? The CSV file obviously has end-of-line markers in it, but I note after this is uploaded they seem to turn into spaces, and the CSV:Reader.parse does not then seem to be able to pick them up. If you have a missing value, e.g. one which is not mandatory, the fields get out of whack. So it seems the issue is in how a file is uploaded via the rails infrastructure and then presented to CSV:Reader.parse I think.

    Any suggestions?

    I’d rather modify the code here to make it more forgiving then have to manually modify ever CSV file.

  • 9. GregH  |  April 24, 2008 at 2:03 am

    PS.

    Oh, I see now that the problem is that the first record is really at “row[0]”, not “row[1]”. This seems to have things working now.

Leave a comment

Trackback this post  |  Subscribe to the comments via RSS Feed


Calendar

July 2007
M T W T F S S
 1
2345678
9101112131415
16171819202122
23242526272829
3031  

Most Recent Posts