Importing csv into Ruby on Rails application error -
i trying able import csv of courses consist of course name , professor. i'm having error i've been trying fix research, i'm coming blank. appreciate if take , me see doing incorrectly.
in routes.rb
get 'import/index' post 'import/index' resources :courses 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 |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 %> <%= file_field_tag :file %> <%= submit_tag "import courses" %> <% end %>
my rake routes:
controller#action import_index /import/index(.:format) import#index post /import/index(.:format) import#index import_courses post /courses/import(.:format) courses#import courses /courses(.:format) courses#index post /courses(.:format) courses#create new_course /courses/new(.:format) courses#new edit_course /courses/:id/edit(.:format) courses#edit course /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 import controller views. in retrospect, not best way - should have made view in course instead. getting no data received "err_empty_response" because redirecting /courses/import, doesn't have view. want route courses page instead. i'm confused , feel might have convoluted situation. if can see why csv import not working correctly, please let me know! thank you.
edit: here error i'm getting log:
[2015-04-29 00:05:38] error uri::invalidurierror: scheme http 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'
course_path
-> should path single, specific course - aren't passing course show... or should courses_path
Comments
Post a Comment