title: how to import csv-file via 'roo' gem?
body: <p>please help import csv-file.</p>
  
  <p>i install gem 'roo' and gem 'iconv'. i make scaffold products nad form for import file:</p>
  
  <pre><code>&lt;%= form_tag import_products_path, multipart: true do %&gt;
    &lt;%= file_field_tag :file %&gt;
    &lt;%= submit_tag "Import" %&gt;
  &lt;% end %&gt;
  </code></pre>
  
  <p>config/routes.rb</p>
  
  <pre><code>resources :products do
    collection { post :import }
  end
  </code></pre>
  
  <p>products_controller.rb</p>
  
  <pre><code>def import
    Product.import(params[:file])
    redirect_to root_url, notice: "Products imported."
  end
  </code></pre>
  
  <p>models/product.rb</p>
  
  <pre><code>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]
      product = find_by_id(row["id"]) || new
      product.attributes = row.to_hash.slice(*accessible_attributes)
      product.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
  </code></pre>
  
  <p>i use this popular tutorial: <a href="http://railscasts.com/episodes/396-importing-csv-and-excel?autoplay=true" rel="nofollow">http://railscasts.com/episodes/396-importing-csv-and-excel?autoplay=true</a> </p>
  
  <p>but after load csv-file via form, browser display follow: </p>
  
  <pre><code>NameError in ProductsController#import
  uninitialized constant Csv
  </code></pre>
  
  <p>and console display follow error </p>
  
  <pre><code>message:#&lt;ActionDispatch::Http::UploadedFile:0x000000060f36a0 @tempfile=#&lt;Tempfile:/tmp/RackMultipart20151015-18667-14nvfde.csv&gt;, @original_filename="products (2).csv", @content_type="text/csv", @headers="Content-Disposition: form-data; name=\"file\"; filename=\"products (2).csv\"\r\nContent-Type: text/csv\r\n"&gt;
  Completed 500 Internal Server Error in 12ms (ActiveRecord: 0.0ms)
  
  NameError (uninitialized constant Csv):
    app/models/product.rb:25:in `open_spreadsheet'
    app/models/product.rb:11:in `import'
    app/controllers/products_controller.rb:82:in `import'
  </code></pre>
  
  <p>i try in model replace:</p>
  
  <pre><code>when ".csv" then Csv.new(file.path, nil, :ignore)
  </code></pre>
  
  <p>on </p>
  
  <pre><code>when '.csv' then Roo::Csv.new(file.path, nil, :ignore) 
  </code></pre>
  
  <p>but problem not solve.</p>
  
  <p>in this topic i asked to Suraj about tweaking, but but received no response.</p>
  
  <p>please help impoer csv-file</p>