将csv导入Ruby on Rails应用程序错误

时间:2015-04-29 05:55:31

标签: ruby-on-rails ruby csv

我目前正在尝试导入一个由课程名称和教授组成的课程csv。我有一个错误,我一直试图解决研究,但我发现空白。如果有人能看一看并帮我看看我做错了什么,我将不胜感激。

在routes.rb

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

resources :courses do
  collection { post :import }
end

在课程中.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

我为视图生成了一个导入控制器。回想起来,这可能不是最好的方式 - 我应该在课程中提出一个观点。我目前收到的数据没有收到&#34; err_empty_response&#34;因为它重定向到/ 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 个答案:

答案 0 :(得分:1)

course_path - &gt;这应该是一个特定课程的路径 - 但你没有把它传递给一个课程......或者应该是courses_path

相关问题