# Mport a csv file to database.(help pls)

**URL:** <https://www.ruby-forum.com/t/mport-a-csv-file-to-database-help-pls/186069>\
**Category:** Engines\
**Created:** [March 31, 2010, 6:22am UTC](https://www.ruby-forum.com/t/mport-a-csv-file-to-database-help-pls/186069 "2010-03-31T06:22:07Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![murfhy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/d26b3c/32.png) [@murfhy](https://www.ruby-forum.com/u/murfhy)\
**Post date:** [March 31, 2010, 6:22am UTC](https://www.ruby-forum.com/t/mport-a-csv-file-to-database-help-pls/186069/1 "2010-03-31T06:22:07Z")

</div>

hi actually i am creating an application to import a csv file to  
database. but onn running that application ai’am getting errors like  
this

NoMethodError in ImportsController#new

undefined method `login\_required’ for #ImportsController:0xb6d84b5c

and this error too

Processing ImportsController#new (for 127.0.0.1 at 2010-03-31 09:38:19)  
[GET]

NoMethodError (undefined method `login\_required’ for  
#ImportsController:0xb6d03f70):

ma controller is here

class ImportsController \< ApplicationController  
before\_filter :login\_required #protect controller from anonymous users

def new  
@import = Import.new  
end

def create  
@import = Import.new(params[:import])

```
respond_to do |format|
  if @import.save!
    flash[:notice] = 'CSV data was successfully imported.'
    format.html { redirect_to(@import) }
  else
    flash[:error] = 'CSV data import failed.'
    format.html { render :action => "new" }
  end
end

```

end

def show  
@import = Import.find(params[:id])  
end

def proc\_csv  
@import = Import.find(params[:id])  
lines = parse\_csv\_file(@import.csv.path)  
lines.shift #comment this line out if your CSV file doesn’t contain  
a header row  
if lines.size \> 0  
@import.processed = lines.size  
lines.each do |line|  
case @import.datatype  
when “releases”  
new\_release(line)  
end  
end  
@import.save  
flash[:notice] = “CSV data processing was successful.”  
redirect\_to :action =\> “show”, :id =\> @import.id  
else  
flash[:error] = “CSV data processing failed.”  
render :action =\> “show”, :id =\> @import.id  
end  
end

private

def parse\_csv\_file(path\_to\_csv)  
lines = []

```
#if not installed run, sudo gem install fastercsv
#http://fastercsv.rubyforge.org/
require 'fastercsv'

FasterCSV.foreach(path_to_csv) do |row|
  lines << row
end
lines

```

end

def new\_release(line)  
params = Hash.new  
params[:release] = Hash.new  
params[:release][“title”] = line[0]  
params[:release][“rdate”] = line[1]  
params[:release][“body”] = line[3]  
params[:release][“notes”] = line[4]  
release = Release.new(params[:release])  
release.save  
end

end

this is ma model

class Import \< ActiveRecord::Base

# Paperclip

has\_attached\_file :csv  
validates\_attachment\_presence :csv  
validates\_attachment\_content\_type :csv, :content\_type =\>  
[‘text/csv’,‘text/comma-separated-values’,‘text/csv’,‘application/csv’,‘application/excel’,‘application/vnd.ms-excel’,‘application/vnd.msexcel’,‘text/anytext’,‘text/plain’]  
end

anyone pls help me
