title: Rails 4 Error in CSV Import
body: <p>I am trying to add a feature in my Rails 4 app that allows a user to import a CSV file. I am following the tutotial on: <a href="http://railscasts.com/episodes/396-importing-csv-and-excel" rel="nofollow">http://railscasts.com/episodes/396-importing-csv-and-excel</a></p>
  
  <p>When I update the necessary files, I get an error <code>no implicit conversion of Symbol into Integer</code> and I'm not sure why, I can't find any help elsewhere.</p>
  
  <p>Here are the code sections appearing in the error page:</p>
  
  <p>file <code>idea.rb</code></p>
  
  <pre><code>class Idea &lt; 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
  </code></pre>
  
  <p>file <code>ideas_controller.rb</code></p>
  
  <pre><code>class IdeasController &lt; 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
  </code></pre>
  
  <p>I am using <code>require 'csv'</code> in <code>application.rb</code></p>
  
  <p>and </p>
  
  <pre><code>resources :ideas do
     collection { post :import }
  end
  </code></pre>
  
  <p>in <code>routes.rb</code></p>
  
  <p>Please let me know if you need more information.</p>
  
  <p><strong>Update: Add Full Trace</strong></p>
  
  <pre><code>/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'
  </code></pre>
  
  <p>**Update: Add <code>print params[:file]</code></p>
  
  <pre><code>2.2.1 :001 &gt; 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 `&lt;top (required)&gt;'
      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 `&lt;top (required)&gt;'
      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 `&lt;top (required)&gt;'
      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'
  </code></pre>
  
  <p><strong>@Kimball - updated using your advice</strong></p>
  
  <p>my model now looks like (i tested the import method in a plain ruby script and it works):</p>
  
  <pre><code>class Idea &lt; ActiveRecord::Base
  
    def self.import(file)
      csv = CSV.open(file, "r")
  
        arr1 = []
        arr2 = []
  
        list = CSV.foreach(file, :col_sep =&gt; ",", :return_headers =&gt; false) do |row|
          arr1 &lt;&lt; row[0]
          arr2 &lt;&lt; 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
  </code></pre>
  
  <p>it is throwing an error of <code>no implicit conversion of ActionDispatch::Http::UploadedFile into String</code></p>
  
  <p>the trace shows the following issues:</p>
  
  <p>in model:</p>
  
  <pre><code> def self.import(file)
       csv = CSV.open(file, "r") #line with issue
         arr1 = []
         arr2 = []
  </code></pre>
  
  <p>in controller:</p>
  
  <pre><code> def import
     Idea.import(params[:file])
     redirect_to :create, notice: "Ideas imported."
   end
  </code></pre>
  
  <p>It seems like it's not recognizing a file is being imported and therefore not able to read it.</p>