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?

-- 
Posted via http://www.ruby-forum.com/.

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to 
rubyonrails-talk+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to