Rails将csv导入数据库

时间:2014-02-26 00:37:16

标签: ruby-on-rails ruby sqlite activerecord

我的问题主要不是如何导入文件。我的控制器中的create方法有问题。我正在导入一些任务,Task.newtask_params作为参数。这种方法如下:

def task_params
      params.require(:task).permit(:date, :description)
end

运行时会出错。我在parameter missing方法中遇到task_params错误。有没有办法解决这个问题并允许文件通过?

编辑以下是完整的跟踪:

actionpack (4.0.2) lib/action_controller/metal/strong_parameters.rb:173:in `require'
app/controllers/tasks_controller.rb:78:in `task_params'
app/controllers/tasks_controller.rb:33:in `create'
actionpack (4.0.2) lib/action_controller/metal/implicit_render.rb:4:in `send_action'
actionpack (4.0.2) lib/abstract_controller/base.rb:189:in `process_action'
actionpack (4.0.2) lib/action_controller/metal/rendering.rb:10:in `process_action'
actionpack (4.0.2) lib/abstract_controller/callbacks.rb:18:in `block in process_action'
activesupport (4.0.2) lib/active_support/callbacks.rb:413:in `_run__2649129199446340827__process_action__callbacks'
activesupport (4.0.2) lib/active_support/callbacks.rb:80:in `run_callbacks'
actionpack (4.0.2) lib/abstract_controller/callbacks.rb:17:in `process_action'
actionpack (4.0.2) lib/action_controller/metal/rescue.rb:29:in `process_action'
actionpack (4.0.2) lib/action_controller/metal/instrumentation.rb:31:in `block in process_action'
activesupport (4.0.2) lib/active_support/notifications.rb:159:in `block in instrument'
activesupport (4.0.2) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
activesupport (4.0.2) lib/active_support/notifications.rb:159:in `instrument'
actionpack (4.0.2) lib/action_controller/metal/instrumentation.rb:30:in `process_action'
actionpack (4.0.2) lib/action_controller/metal/params_wrapper.rb:245:in `process_action'
activerecord (4.0.2) lib/active_record/railties/controller_runtime.rb:18:in `process_action'
actionpack (4.0.2) lib/abstract_controller/base.rb:136:in `process'
actionpack (4.0.2) lib/abstract_controller/rendering.rb:44:in `process'
actionpack (4.0.2) lib/action_controller/metal.rb:195:in `dispatch'
actionpack (4.0.2) lib/action_controller/metal/rack_delegation.rb:13:in `dispatch'
actionpack (4.0.2) lib/action_controller/metal.rb:231:in `block in action'
actionpack (4.0.2) lib/action_dispatch/routing/route_set.rb:80:in `call'
actionpack (4.0.2) lib/action_dispatch/routing/route_set.rb:80:in `dispatch'
actionpack (4.0.2) lib/action_dispatch/routing/route_set.rb:48:in `call'
actionpack (4.0.2) lib/action_dispatch/journey/router.rb:71:in `block in call'
actionpack (4.0.2) lib/action_dispatch/journey/router.rb:59:in `each'
actionpack (4.0.2) lib/action_dispatch/journey/router.rb:59:in `call'
actionpack (4.0.2) lib/action_dispatch/routing/route_set.rb:680:in `call'
/usr/lib/ruby/vendor_ruby/rack/etag.rb:23:in `call'
/usr/lib/ruby/vendor_ruby/rack/conditionalget.rb:35:in `call'
/usr/lib/ruby/vendor_ruby/rack/head.rb:11:in `call'
actionpack (4.0.2) lib/action_dispatch/middleware/params_parser.rb:27:in `call'
actionpack (4.0.2) lib/action_dispatch/middleware/flash.rb:241:in `call'
/usr/lib/ruby/vendor_ruby/rack/session/abstract/id.rb:225:in `context'
/usr/lib/ruby/vendor_ruby/rack/session/abstract/id.rb:220:in `call'
actionpack (4.0.2) lib/action_dispatch/middleware/cookies.rb:486:in `call'
activerecord (4.0.2) lib/active_record/query_cache.rb:36:in `call'
activerecord (4.0.2) lib/active_record/connection_adapters/abstract/connection_pool.rb:626:in `call'
activerecord (4.0.2) lib/active_record/migration.rb:369:in `call'
actionpack (4.0.2) lib/action_dispatch/middleware/callbacks.rb:29:in `block in call'
activesupport (4.0.2) lib/active_support/callbacks.rb:373:in `_run__104897348271024377__call__callbacks'
activesupport (4.0.2) lib/active_support/callbacks.rb:80:in `run_callbacks'
actionpack (4.0.2) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
actionpack (4.0.2) lib/action_dispatch/middleware/reloader.rb:64:in `call'
actionpack (4.0.2) lib/action_dispatch/middleware/remote_ip.rb:76:in `call'
actionpack (4.0.2) lib/action_dispatch/middleware/debug_exceptions.rb:17:in `call'
actionpack (4.0.2) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
railties (4.0.2) lib/rails/rack/logger.rb:38:in `call_app'
railties (4.0.2) lib/rails/rack/logger.rb:20:in `block in call'
activesupport (4.0.2) lib/active_support/tagged_logging.rb:67:in `block in tagged'
activesupport (4.0.2) lib/active_support/tagged_logging.rb:25:in `tagged'
activesupport (4.0.2) lib/active_support/tagged_logging.rb:67:in `tagged'
railties (4.0.2) lib/rails/rack/logger.rb:20:in `call'
actionpack (4.0.2) lib/action_dispatch/middleware/request_id.rb:21:in `call'
/usr/lib/ruby/vendor_ruby/rack/methodoverride.rb:21:in `call'
/usr/lib/ruby/vendor_ruby/rack/runtime.rb:17:in `call'
activesupport (4.0.2) lib/active_support/cache/strategy/local_cache.rb:83:in `call'
/usr/lib/ruby/vendor_ruby/rack/lock.rb:17:in `call'
actionpack (4.0.2) lib/action_dispatch/middleware/static.rb:64:in `call'
/usr/lib/ruby/vendor_ruby/rack/sendfile.rb:112:in `call'
railties (4.0.2) lib/rails/engine.rb:511:in `call'
railties (4.0.2) lib/rails/application.rb:97:in `call'
/usr/lib/ruby/vendor_ruby/rack/lock.rb:17:in `call'
/usr/lib/ruby/vendor_ruby/rack/content_length.rb:14:in `call'
/usr/lib/ruby/vendor_ruby/rack/handler/webrick.rb:60:in `service'
/usr/lib/ruby/1.9.1/webrick/httpserver.rb:138:in `service'
/usr/lib/ruby/1.9.1/webrick/httpserver.rb:94:in `run'
/usr/lib/ruby/1.9.1/webrick/server.rb:191:in `block in start_thread'

导入方法:

  def import
      Task.import(params[:file])
      redirect_to tasks_path, notice: "Tasks Imported Successfully"
  end

然后Task模型具有以下内容:

def self.import(file)
    spreadsheet = CSV.new(file.path, nil, :ignore)
    Task.create! row.to_hash
    header = spreadsheet.row(1)
    (2..spreadsheet.last_row).each do |i|
        row = Hash[[header, spreadsheet.row(i)].transpose]
        task = find_by_id(row['id']) || new
        task.attributes = row.to_hash.slice(*accessible_attributes)
        task.save
    end
end

2 个答案:

答案 0 :(得分:0)

这些行(适当调整到您的代码)应该可以在控制器的“创建”操作中跳过验证

@task=Task.new(task_params)
@task.save(:validate => false)

但是,跳过验证几乎不值得推荐......

答案 1 :(得分:0)

好的家伙解决了。主要问题是进口形式。如果有人遇到同样的问题,请确保您重定向到action中的正确controller。这是我以前的表格标签:

<%= form_tag tasks_path, multipart: true do %>

但这不正确。如果您已定义一个,则应将其重定向到控制器中的import操作:

<%= form_tag import_tasks_path, multipart: true do %>

您可以通过运行action

确保已定义此rake routes |grep import

我希望这很有用