2015-04-29 44 views
0

我目前正試圖導入包含課程名稱和教授的課程的csv。我有一個錯誤,我一直在努力解決研究問題,但我會空白。我會很感激,如果有人可以看看,並幫助我看看我做錯了什麼。將csv導入Ruby on Rails應用程序錯誤

在routes.rb中

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

resources :courses do 
    collection { post :import } 
end 

在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 

在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 

在進口指數視圖:

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

我耙路線:

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 

我產生了意見進口控制器。回想起來,這可能不是最好的方式 - 我應該在過程中改變看法。我目前正在收到沒有收到數據的「err_empty_response」,因爲它正在重定向到/ courses/import,這沒有視圖。我想讓它返回到課程頁面。我很困惑,覺得我可能會讓情況變得複雜。如果有人可以看到爲什麼csv導入無法正常工作,請告訴我!謝謝。

編輯: 這是我從日誌中發現了錯誤:

[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' 
+1

嗨,你能告訴我們錯誤消息和你的服務器日誌中stacktrace的第幾行(錯誤後的幾行guff)?另外:您的courses_controller是否有索引操作?你說你創建了一個import_controller,但是你向我們展示了一個courses_controller?你能否清楚你現在實際上有什麼? :)另外:'course_path' - >這應該是一個單一的特定課程的路徑 - 但你沒有通過它的課程來顯示......應該是'courses_path'嗎? –

+1

嗨,謝謝你回到我身邊。請參閱我的編輯錯誤消息。是的,這是一個索引操作,它只是列出了所有的課程。我爲視圖創建了一個import_controller,但沒有使用任何操作,所以我可能會刪除它。是 - 將路徑更新爲courses_path修復錯誤。小東西!!謝謝! – AHalbert

+0

好吧,我會把它移到答案部分:) –

回答

1

course_path - >這應該是一個單一的,特定的路線的道路 - 但你不能傳遞一個過程以顯示...或應該是courses_path