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?