【发布时间】:2015-05-29 02:54:17
【问题描述】:
我正在尝试在我的 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 脚本中测试了导入方法并且它有效):
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
它似乎无法识别正在导入的文件,因此无法读取它。
【问题讨论】:
-
您能否发布堆栈跟踪,以便我们查看导致异常的行/语句?
-
我添加了完整的跟踪。我相信这就是你所要求的。我还是 Rails 新手,
-
params[:file] 有什么作用?我不确定它是否有路径方法。尝试将 params[:file] 对象打印到控制台以确保它符合您的期望。另外,您应该为此使用 strong_parameters 吗?
-
我添加了来自
print params[:file]的输出。我不确定我在这里期待什么。我从 strong_params 中删除了 `:file',但没有任何改变。 -
抱歉,我的意思是添加 params[:file] 到控制器以便它打印到控制台
标签: ruby-on-rails ruby csv ruby-on-rails-4