簡體   English   中英

CSV導入中的Rails 4錯誤

[英]Rails 4 Error in CSV Import

我正在嘗試在Rails 4應用程序中添加一項功能,該功能允許用戶導入CSV文件。 我正在關注以下內容: http ://railscasts.com/episodes/396-importing-csv-and-excel

更新必要的文件時,出現錯誤, no implicit conversion of Symbol into Integer而且我不確定為什么,在其他地方找不到任何幫助。

以下是錯誤頁面上顯示的代碼部分:

文件idea.rb

class Idea < ActiveRecord::Base

  def self.import(file)
    spreadsheet = open_spreadsheet(file)
    header = spreadsheet.row(1)
    (2..spreadsheet.last_row).each do |i|
      row = Hash[[header, spreadsheet.row(i)].transpose]
      #idea = find_by_id(row["id"]) || new
      idea.attributes = row.to_hash.slice(*accessible_attributes)
      idea.save!
    end
  end

  def self.open_spreadsheet(file)
    case File.extname(file.original_filename)
    when ".csv" then CSV.open(file.path, nil, :ignore)
    when ".xls" then Excel.new(file.path, nil, :ignore)
    when ".xlsx" then Excelx.new(file.path, nil, :ignore)
    else raise "Unknown file type: #{file.original_filename}"
    end
  end

end

文件ideas_controller.rb

class IdeasController < ApplicationController
  def index
  end

  def new
    @idea = Idea.new
  end

  def create
    @idea = Idea.new(idea_params)

    if @idea.save
      render :index
    else
      render 'new'
    end
  end

  def import
    Idea.import(params[:file])
    redirect_to root_url, notice: "Ideas imported."
  end

  private

  def idea_params
    params.require(:idea).permit(:idea_list, :listA, :listB, :file)
  end
end

我在application.rb使用require 'csv'

resources :ideas do
   collection { post :import }
end

routes.rb

如果您需要更多信息,請告訴我。

更新:添加完整跟蹤

/Users/jdesilvio/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/csv.rb:1256:in `initialize'
/Users/jdesilvio/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/csv.rb:1256:in `open'
/Users/jdesilvio/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/csv.rb:1256:in `open'
app/models/idea.rb:16:in `open_spreadsheet'
app/models/idea.rb:4:in `import'
app/controllers/ideas_controller.rb:20:in `import'
actionpack (4.2.1) lib/action_controller/metal/implicit_render.rb:4:in `send_action'
actionpack (4.2.1) lib/abstract_controller/base.rb:198:in `process_action'
actionpack (4.2.1) lib/action_controller/metal/rendering.rb:10:in `process_action'
actionpack (4.2.1) lib/abstract_controller/callbacks.rb:20:in `block in process_action'
activesupport (4.2.1) lib/active_support/callbacks.rb:117:in `call'
activesupport (4.2.1) lib/active_support/callbacks.rb:117:in `call'
activesupport (4.2.1) lib/active_support/callbacks.rb:555:in `block (2 levels) in compile'
activesupport (4.2.1) lib/active_support/callbacks.rb:505:in `call'
activesupport (4.2.1) lib/active_support/callbacks.rb:505:in `call'
activesupport (4.2.1) lib/active_support/callbacks.rb:92:in `_run_callbacks'
activesupport (4.2.1) lib/active_support/callbacks.rb:776:in `_run_process_action_callbacks'
activesupport (4.2.1) lib/active_support/callbacks.rb:81:in `run_callbacks'
actionpack (4.2.1) lib/abstract_controller/callbacks.rb:19:in `process_action'
actionpack (4.2.1) lib/action_controller/metal/rescue.rb:29:in `process_action'
actionpack (4.2.1) lib/action_controller/metal/instrumentation.rb:32:in `block in process_action'
activesupport (4.2.1) lib/active_support/notifications.rb:164:in `block in instrument'
activesupport (4.2.1) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
activesupport (4.2.1) lib/active_support/notifications.rb:164:in `instrument'
actionpack (4.2.1) lib/action_controller/metal/instrumentation.rb:30:in `process_action'
actionpack (4.2.1) lib/action_controller/metal/params_wrapper.rb:250:in `process_action'
activerecord (4.2.1) lib/active_record/railties/controller_runtime.rb:18:in `process_action'
actionpack (4.2.1) lib/abstract_controller/base.rb:137:in `process'
actionview (4.2.1) lib/action_view/rendering.rb:30:in `process'
actionpack (4.2.1) lib/action_controller/metal.rb:196:in `dispatch'
actionpack (4.2.1) lib/action_controller/metal/rack_delegation.rb:13:in `dispatch'
actionpack (4.2.1) lib/action_controller/metal.rb:237:in `block in action'
actionpack (4.2.1) lib/action_dispatch/routing/route_set.rb:74:in `call'
actionpack (4.2.1) lib/action_dispatch/routing/route_set.rb:74:in `dispatch'
actionpack (4.2.1) lib/action_dispatch/routing/route_set.rb:43:in `serve'
actionpack (4.2.1) lib/action_dispatch/journey/router.rb:43:in `block in serve'
actionpack (4.2.1) lib/action_dispatch/journey/router.rb:30:in `each'
actionpack (4.2.1) lib/action_dispatch/journey/router.rb:30:in `serve'
actionpack (4.2.1) lib/action_dispatch/routing/route_set.rb:819:in `call'
rack (1.6.1) lib/rack/etag.rb:24:in `call'
rack (1.6.1) lib/rack/conditionalget.rb:38:in `call'
rack (1.6.1) lib/rack/head.rb:13:in `call'
actionpack (4.2.1) lib/action_dispatch/middleware/params_parser.rb:27:in `call'
actionpack (4.2.1) lib/action_dispatch/middleware/flash.rb:260:in `call'
rack (1.6.1) lib/rack/session/abstract/id.rb:225:in `context'
rack (1.6.1) lib/rack/session/abstract/id.rb:220:in `call'
actionpack (4.2.1) lib/action_dispatch/middleware/cookies.rb:560:in `call'
activerecord (4.2.1) lib/active_record/query_cache.rb:36:in `call'
activerecord (4.2.1) lib/active_record/connection_adapters/abstract/connection_pool.rb:649:in `call'
activerecord (4.2.1) lib/active_record/migration.rb:378:in `call'
actionpack (4.2.1) lib/action_dispatch/middleware/callbacks.rb:29:in `block in call'
activesupport (4.2.1) lib/active_support/callbacks.rb:88:in `call'
activesupport (4.2.1) lib/active_support/callbacks.rb:88:in `_run_callbacks'
activesupport (4.2.1) lib/active_support/callbacks.rb:776:in `_run_call_callbacks'
activesupport (4.2.1) lib/active_support/callbacks.rb:81:in `run_callbacks'
actionpack (4.2.1) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
actionpack (4.2.1) lib/action_dispatch/middleware/reloader.rb:73:in `call'
actionpack (4.2.1) lib/action_dispatch/middleware/remote_ip.rb:78:in `call'
actionpack (4.2.1) lib/action_dispatch/middleware/debug_exceptions.rb:17:in `call'
web-console (2.1.2) lib/web_console/middleware.rb:37:in `call'
actionpack (4.2.1) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
railties (4.2.1) lib/rails/rack/logger.rb:38:in `call_app'
railties (4.2.1) lib/rails/rack/logger.rb:20:in `block in call'
activesupport (4.2.1) lib/active_support/tagged_logging.rb:68:in `block in tagged'
activesupport (4.2.1) lib/active_support/tagged_logging.rb:26:in `tagged'
activesupport (4.2.1) lib/active_support/tagged_logging.rb:68:in `tagged'
railties (4.2.1) lib/rails/rack/logger.rb:20:in `call'
actionpack (4.2.1) lib/action_dispatch/middleware/request_id.rb:21:in `call'
rack (1.6.1) lib/rack/methodoverride.rb:22:in `call'
rack (1.6.1) lib/rack/runtime.rb:18:in `call'
activesupport (4.2.1) lib/active_support/cache/strategy/local_cache_middleware.rb:28:in `call'
rack (1.6.1) lib/rack/lock.rb:17:in `call'
actionpack (4.2.1) lib/action_dispatch/middleware/static.rb:113:in `call'
rack (1.6.1) lib/rack/sendfile.rb:113:in `call'
railties (4.2.1) lib/rails/engine.rb:518:in `call'
railties (4.2.1) lib/rails/application.rb:164:in `call'
rack (1.6.1) lib/rack/lock.rb:17:in `call'
rack (1.6.1) lib/rack/content_length.rb:15:in `call'
rack (1.6.1) lib/rack/handler/webrick.rb:89:in `service'
/Users/jdesilvio/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/webrick/httpserver.rb:138:in `service'
/Users/jdesilvio/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/webrick/httpserver.rb:94:in `run'
/Users/jdesilvio/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/webrick/server.rb:294:in `block in start_thread'

**更新:添加print params[:file]

2.2.1 :001 > print params[:file]
NameError: undefined local variable or method `params' for main:Object
    from (irb):1
    from /Users/jdesilvio/.rvm/gems/ruby-2.2.1@global/gems/railties-4.2.1/lib/rails/commands/console.rb:110:in `start'
    from /Users/jdesilvio/.rvm/gems/ruby-2.2.1@global/gems/railties-4.2.1/lib/rails/commands/console.rb:9:in `start'
    from /Users/jdesilvio/.rvm/gems/ruby-2.2.1@global/gems/railties-4.2.1/lib/rails/commands/commands_tasks.rb:68:in `console'
    from /Users/jdesilvio/.rvm/gems/ruby-2.2.1@global/gems/railties-4.2.1/lib/rails/commands/commands_tasks.rb:39:in `run_command!'
    from /Users/jdesilvio/.rvm/gems/ruby-2.2.1@global/gems/railties-4.2.1/lib/rails/commands.rb:17:in `<top (required)>'
    from /Users/jdesilvio/.rvm/gems/ruby-2.2.1/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:274:in `require'
    from /Users/jdesilvio/.rvm/gems/ruby-2.2.1/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:274:in `block in require'
    from /Users/jdesilvio/.rvm/gems/ruby-2.2.1/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:240:in `load_dependency'
    from /Users/jdesilvio/.rvm/gems/ruby-2.2.1/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:274:in `require'
    from /Users/jdesilvio/workspace/ideasex/bin/rails:8:in `<top (required)>'
    from /Users/jdesilvio/.rvm/gems/ruby-2.2.1/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:268:in `load'
    from /Users/jdesilvio/.rvm/gems/ruby-2.2.1/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:268:in `block in load'
    from /Users/jdesilvio/.rvm/gems/ruby-2.2.1/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:240:in `load_dependency'
    from /Users/jdesilvio/.rvm/gems/ruby-2.2.1/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:268:in `load'
    from /Users/jdesilvio/.rvm/gems/ruby-2.2.1/gems/spring-1.3.6/lib/spring/commands/rails.rb:6:in `call'
    from /Users/jdesilvio/.rvm/gems/ruby-2.2.1/gems/spring-1.3.6/lib/spring/command_wrapper.rb:38:in `call'
    from /Users/jdesilvio/.rvm/gems/ruby-2.2.1/gems/spring-1.3.6/lib/spring/application.rb:183:in `block in serve'
    from /Users/jdesilvio/.rvm/gems/ruby-2.2.1/gems/spring-1.3.6/lib/spring/application.rb:156:in `fork'
    from /Users/jdesilvio/.rvm/gems/ruby-2.2.1/gems/spring-1.3.6/lib/spring/application.rb:156:in `serve'
    from /Users/jdesilvio/.rvm/gems/ruby-2.2.1/gems/spring-1.3.6/lib/spring/application.rb:131:in `block in run'
    from /Users/jdesilvio/.rvm/gems/ruby-2.2.1/gems/spring-1.3.6/lib/spring/application.rb:125:in `loop'
    from /Users/jdesilvio/.rvm/gems/ruby-2.2.1/gems/spring-1.3.6/lib/spring/application.rb:125:in `run'
    from /Users/jdesilvio/.rvm/gems/ruby-2.2.1/gems/spring-1.3.6/lib/spring/application/boot.rb:18:in `<top (required)>'
    from /Users/jdesilvio/.rvm/rubies/ruby-2.2.1/lib/ruby/site_ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require'
    from /Users/jdesilvio/.rvm/rubies/ruby-2.2.1/lib/ruby/site_ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require'

@Kimball-使用您的建議進行更新

我的模型現在看起來像(我在一個簡單的ruby腳本中測試了import方法,它可以工作):

class Idea < ActiveRecord::Base

  def self.import(file)
    csv = CSV.open(file, "r")

      arr1 = []
      arr2 = []

      list = CSV.foreach(file, :col_sep => ",", :return_headers => false) do |row|
        arr1 << row[0]
        arr2 << row[1]
      end
    csv.close

    idea.listA = arr1.drop(1)
    idea.listB = arr2.drop(1)
    idea.save!
  end

  def self.open_spreadsheet(file)
    case File.extname(file.original_filename)
    when ".csv" then CSV.new(file.path)
    else raise "Unknown file type: #{file.original_filename}"
    end
  end

end

它拋出錯誤,即no implicit conversion of ActionDispatch::Http::UploadedFile into String

跟蹤顯示以下問題:

在模型中:

 def self.import(file)
     csv = CSV.open(file, "r") #line with issue
       arr1 = []
       arr2 = []

在控制器中:

 def import
   Idea.import(params[:file])
   redirect_to :create, notice: "Ideas imported."
 end

似乎無法識別文件正在導入,因此無法讀取它。

通過在任何CSV上調用CSV.open(file.path, nil, :ignore)都會得到該錯誤,例如:

require 'csv'

# open a new CSV and write a couple of rows
CSV.open("test.csv", "w") do |csv|
  csv.puts ["test", "header"]
  csv.puts ["test", "data"]
end

# now try and open that same CSV with 2nd and 3rd args nil and :ignore
csv = CSV.open("test.csv", nil, :ignore)
# => TypeError: no implicit conversion of Symbol into Integer

此處記錄 CSV::open (對於Ruby 1.9.2)。 第二個參數應為開放模式(例如,讀取,寫入,附加)-請參見SO列表 -第三個參數可以為選項哈希。 繼續上述操作,您可以嘗試以下操作:

csv = CSV.open("test.csv", "r")
csv.readline # note there is no CSV#row method
# => ["test", "header"]
csv.readline
# => ["test", "data"}
csv.close # if you're not passing a block to CSV::open, be sure to close the file

請注意,還有一個CSV::read方法,它更適合處理CSV文件中的標題行-請通讀文檔以了解更多信息。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM