# Undefined method 'read' in importing CSV file

**URL:** <https://www.ruby-forum.com/t/undefined-method-read-in-importing-csv-file/223262>\
**Category:** Rails\
**Created:** [September 27, 2012, 5:25am UTC](https://www.ruby-forum.com/t/undefined-method-read-in-importing-csv-file/223262 "2012-09-27T05:25:01Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![aris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/258eb7/32.png) [@aris](https://www.ruby-forum.com/u/aris)\
**Post date:** [September 27, 2012, 5:25am UTC](https://www.ruby-forum.com/t/undefined-method-read-in-importing-csv-file/223262/1 "2012-09-27T05:25:01Z")

</div>

Rails 3.1.3

I am trying to import CSV file and upload database accordingly.

the view is the following.

```
  <%= form_tag( { :action => "import_csvdata"},
    :html => { :multipart => true }) do |f| %>
    <label for="file">
        Select a CSV File :
    </label>
    <%= file_field_tag :file %>
    <%= submit_tag 'Submit', :class => 'btn btn-large btn-success'

```

%\>  
\<% end %\>

the controller is,

def import\_csvdata  
if request.post? && params[:file].present?  
infile = params[:file].read ####\<===HERE!!!  
n, errs = 0, []

```
  CSV.parse(infile) do |row|
    n += 1
    # SKIP: header i.e. first row OR blank row
    next if n == 1 or row.join.blank?
    @departure = Departure.find_or_create_by_city(row[2])
    @destination = Destination.find_or_create_by_city(row[3])
    @airline = Airline.find_or_create_by_company(row[0])

    @flight_name = FlightName.new(:name => row[1],
                                  :airline_id => @airline.id,
                                  :departure_id => @departure.id,
                                  :destination_id =>

```

@destination.id)  
@flight\_name.save

```
  end
end
redirect_to airlines_url

```

end

raises an error  
undefined method `read’ for “first\_test.csv”:String

Do I need to ‘open’ the file? But I searched the web, and it doesn’t  
seem like it.

Could anyone suggest a solution to this problem?
