CSV Data Import To MYSQL DB Using RUBY -
i trying insert data via csv database. controller code
require 'csv' def index end def add @filename=csv::reader.parse(params[:dump][:file]) n=0 csv::foreach(@filename, :headers => true) |row| student.new(row.to_hash.symbolize_keys).save n=n+1 end flash.now[:message]="csv import successful, #{n} new records added data base" end
now when inserting csv file getting error
**can't convert csv::ioreader string**
my ruby version 1.8.7
any appreciated.
if using ruby, use following code:
require 'csv' def index end def add n=0 csv::foreach(params[:dump][:file]) |row| student.new(row.to_hash.symbolize_keys).save n=n+1 end flash.now[:message]="csv import successful, #{n} new records added data base" end
you can more information csv:foreach on http://apidock.com/ruby/csv
i failed above reply because assumed [:dump][:file] path file (string object), seeing error, should read file first (it tempfile object), , parse content string:
require 'csv' def index end def add n=0 csv::parse(params[:dump][:file].read, :headers => true) |row| student.new(row.to_hash.symbolize_keys).save n=n+1 end flash.now[:message]="csv import successful, #{n} new records added data base" end
Comments
Post a Comment