title: uninitialized constant Contact::Csv
body: <p>NOTE: In my application.rb file I have required both <code>csv</code> and <code>iconv</code>.</p>
  
  <p>I am building a Ruby on Rails app to import .xls and .csv files into a database of contacts. I am using Rails Cast #396 as my guild, but it has some issues since I am using Rails 4.</p>
  
  <p>I have implemented (to the best of my ability) the ability to upload .csv, .xls, and .xlsx files. However, when I select a .csv file for upload, I get the following error and stack trace: </p>
  
  <pre><code>uninitialized constant Contact::Csv
  
  Rails.root: C:/workspace/contactually-lite
  
  Application Trace | Framework Trace | Full Trace
  app/models/contact.rb:18:in `open_spreadsheet'
  app/models/contact.rb:6:in `import'
  app/controllers/contacts_controller.rb:5:in `import'
  </code></pre>
  
  <p>Here is my contact.rb file:</p>
  
  <pre><code>class Contact &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]
        contact = find_by_id(row["id"]) || new
        contact.attributes = row.to_hash.slice(*accessible_attributes)
        contact.save!
      end
    end
  
    def self.open_spreadsheet(file)
      case File.extname(file.original_filename)
        when ".csv" then Csv.new(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>Here is my <code>contact_controller.rb</code> file:</p>
  
  <pre><code>class ContactsController &lt; ApplicationController
    before_action :set_contact, only: [:show, :edit, :update, :destroy]
  
    def import
      Contact.import(params[:file])
      redirect_to root_url, notice: "Contacts imported."
    end
  
    # the following code taken from RailsCast 396, may be useful
    # def index
    #   @products = Product.order(:name)
    #   respond_to do |format|
    #     format.html
    #     format.csv { send_data @products.to_csv }
    #     format.xls # { send_data @products.to_csv(col_sep: "\t") }
    #   end
    # end
  
    # GET /contacts
    # GET /contacts.json
    def index
      @contacts = Contact.all
    end
  
    # GET /contacts/1
    # GET /contacts/1.json
    def show
    end
  
    # GET /contacts/new
    def new
      @contact = Contact.new
    end
  
    # GET /contacts/1/edit
    def edit
    end
  
    # POST /contacts
    # POST /contacts.json
    def create
      @contact = Contact.new(contact_params)
  
      respond_to do |format|
        if @contact.save
          format.html { redirect_to @contact, notice: 'Contact was successfully created.' }
          format.json { render :show, status: :created, location: @contact }
        else
          format.html { render :new }
          format.json { render json: @contact.errors, status: :unprocessable_entity }
        end
      end
    end
  
    # PATCH/PUT /contacts/1
    # PATCH/PUT /contacts/1.json
    def update
      respond_to do |format|
        if @contact.update(contact_params)
          format.html { redirect_to @contact, notice: 'Contact was successfully updated.' }
          format.json { render :show, status: :ok, location: @contact }
        else
          format.html { render :edit }
          format.json { render json: @contact.errors, status: :unprocessable_entity }
        end
      end
    end
  
    # DELETE /contacts/1
    # DELETE /contacts/1.json
    def destroy
      @contact.destroy
      respond_to do |format|
        format.html { redirect_to contacts_url, notice: 'Contact was successfully destroyed.' }
        format.json { head :no_content }
      end
    end
  
    private
      # Use callbacks to share common setup or constraints between actions.
      def set_contact
        @contact = Contact.find(params[:id])
      end
  
      # Never trust parameters from the scary internet, only allow the white list through.
      def contact_params
        params.require(:contact).permit(:first_name, :last_name, :email_address)
      end
  end
  </code></pre>
  
  <p>Here is my <code>routes.rb</code> file:</p>
  
  <pre><code>Rails.application.routes.draw do
    # generated by scaffold
    # resources :contacts
  
    # taken from railscast 396
    resources :contacts do
      collection { post :import }
    end
  
    # You can have the root of your site routed with "root"
    root 'contacts#index'
  
  end
  </code></pre>
  
  <p>UPDATE:</p>
  
  <p>Here is my index.html.erb view:</p>
  
  <pre><code>&lt;p id="notice"&gt;&lt;%= notice %&gt;&lt;/p&gt;
  
  &lt;h1&gt;Listing Contacts&lt;/h1&gt;
  
  &lt;table&gt;
    &lt;thead&gt;
      &lt;tr&gt;
        &lt;th&gt;First name&lt;/th&gt;
        &lt;th&gt;Last name&lt;/th&gt;
        &lt;th&gt;Email address&lt;/th&gt;
        &lt;th colspan="3"&gt;&lt;/th&gt;
      &lt;/tr&gt;
    &lt;/thead&gt;
  
    &lt;tbody&gt;
      &lt;% @contacts.each do |contact| %&gt;
        &lt;tr&gt;
          &lt;td&gt;&lt;%= contact.first_name %&gt;&lt;/td&gt;
          &lt;td&gt;&lt;%= contact.last_name %&gt;&lt;/td&gt;
          &lt;td&gt;&lt;%= contact.email_address %&gt;&lt;/td&gt;
          &lt;td&gt;&lt;%= link_to 'Show', contact %&gt;&lt;/td&gt;
          &lt;td&gt;&lt;%= link_to 'Edit', edit_contact_path(contact) %&gt;&lt;/td&gt;
          &lt;td&gt;&lt;%= link_to 'Destroy', contact, method: :delete, data: { confirm: 'Are you sure?' } %&gt;&lt;/td&gt;
        &lt;/tr&gt;
      &lt;% end %&gt;
    &lt;/tbody&gt;
  &lt;/table&gt;
  
  &lt;br&gt;
  
  &lt;%= link_to 'New Contact', new_contact_path %&gt;
  
  &lt;h2&gt;Import Contacts&lt;/h2&gt;
  
  &lt;%= form_tag import_contacts_path, multipart: true do %&gt;
      &lt;%= file_field_tag :file %&gt;
      &lt;%= submit_tag "Import" %&gt;
  &lt;% end %&gt;
  </code></pre>