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'
嗨,你能告诉我们错误消息和你的服务器日志中stacktrace的第几行(错误后的几行guff)?另外:您的courses_controller是否有索引操作?你说你创建了一个import_controller,但是你向我们展示了一个courses_controller?你能否清楚你现在实际上有什么? :)另外:'course_path' - >这应该是一个单一的特定课程的路径 - 但你没有通过它的课程来显示......应该是'courses_path'吗? –
嗨,谢谢你回到我身边。请参阅我的编辑错误消息。是的,这是一个索引操作,它只是列出了所有的课程。我为视图创建了一个import_controller,但没有使用任何操作,所以我可能会删除它。是 - 将路径更新为courses_path修复错误。小东西!!谢谢! – AHalbert
好吧,我会把它移到答案部分:) –