scoots
scoots

Reputation: 294

Importing csv into Ruby on Rails application error

I am currently trying to be able to import a csv of Courses which consist of a course name and a professor. I'm having an error that I've been trying to fix with research, but I'm coming up blank. I would appreciate it if someone could take a look and help me see what I am doing incorrectly.

in routes.rb

get 'import/index'
post 'import/index'

resources :courses do
  collection { post :import }
end

In course.rb:

def self.import(file)
    csv_text = File.read(file.path)
    csv = CSV.parse(csv_text, headers: true) 
    csv.each do |row|
        Course.create!(row.to_hash)
    end 
end

In courses_controller.rb

def import
  Course.import(params[:file])
  redirect_to course_path, notice: "Courses Imported!"
rescue 
  redirect_to 'index', notice: "Invalid file format"
end

In import index view:

<p>Import Course CSV:</p>
<%= form_tag import_courses_path, multipart: true do %>
  <%= file_field_tag :file %>
  <%= submit_tag "Import Courses" %>
<% end %>

my rake routes:

Controller#Action
        import_index GET    /import/index(.:format)                     import#index
                     POST   /import/index(.:format)                     import#index
      import_courses POST   /courses/import(.:format)                   courses#import
             courses GET    /courses(.:format)                          courses#index
                     POST   /courses(.:format)                          courses#create
          new_course GET    /courses/new(.:format)                      courses#new
         edit_course GET    /courses/:id/edit(.:format)                 courses#edit
              course GET    /courses/:id(.:format)                      courses#show
                     PATCH  /courses/:id(.:format)                      courses#update
                     PUT    /courses/:id(.:format)                      courses#update
                     DELETE /courses/:id(.:format)                      courses#destroy

I generated an import controller for the views. In retrospect, this was probably not the best way - I should have made a view in course instead. I am currently getting a No data received "err_empty_response" because it is redirecting to /courses/import, which doesn't have a view. I want it to route back to the courses page instead. I'm confused and feel like I might have convoluted the situation. If anyone can see why the csv import is not working correctly, please let me know! Thank you.

EDIT: Here is the error I'm getting from the log:

[2015-04-29 00:05:38] ERROR URI::InvalidURIError: the scheme http does not accept registry part: localhost:3000index (or bad hostname?)
C:/RailsInstaller/Ruby2.0.0/lib/ruby/2.0.0/uri/generic.rb:1203:in `rescue in merge'
C:/RailsInstaller/Ruby2.0.0/lib/ruby/2.0.0/uri/generic.rb:1200:in `merge'

C:/RailsInstaller/Ruby2.0.0/lib/ruby/2.0.0/webrick/httpresponse.rb:275:in `setup_header'

C:/RailsInstaller/Ruby2.0.0/lib/ruby/2.0.0/webrick/httpresponse.rb:205:in `send_response'
C:/RailsInstaller/Ruby2.0.0/lib/ruby/2.0.0/webrick/httpserver.rb:110:in `run'
C:/RailsInstaller/Ruby2.0.0/lib/ruby/2.0.0/webrick/server.rb:295:in `block in start_thread'

Upvotes: 0

Views: 332

Answers (1)

Taryn East
Taryn East

Reputation: 27747

course_path -> that should be the path for a single, specific course - but you aren't passing it a course to show... or that should be courses_path

Upvotes: 1

Related Questions