[英]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.