Getting uninitialized constant Student :: Roo error while importing csv file in rails
I have an application where I want to provide a function to import records from CSV and Excel file formats. I use the roo
gem for this , but during import it gives the error "uninitialized constant Student :: Roo".
Here is the code:
student.rb
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 Roo::Csv.new(file.path, nil, :ignore)
when ".xls" then Roo::Excel.new(file.path, nil, :ignore)
when ".xlsx" then Roo::Excelx.new(file.path, nil, :ignore)
else raise "Unknown file type: #{file.original_filename}"
end
end
student_controller.rb:
def import
Student.import(params[:file])
#puts @session[:current_organization_id].inspect
redirect_to students_path, notice: "Record imported Successfully."
end
new.html.erb:
<%= form_tag import_students_path, multipart: true do %>
<%= file_field_tag :file , :required=> true%> <br/>
<%= submit_tag "Import" , :class => "btn btn-primary btn-block" %>
<% end %>
Gemfile:
source 'https://rubygems.org'
gem 'rails', '4.2.0'
gem 'mysql2'
gem 'sass-rails', '~> 5.0'
gem 'uglifier', '>= 1.3.0'
gem 'coffee-rails', '~> 4.1.0'
gem 'therubyracer', platforms: :ruby
gem 'jquery-rails'
gem 'jbuilder', '~> 2.0'
gem 'sdoc', '~> 0.4.0', group: :doc
gem 'sass', '3.2.19'
gem 'bower-rails'
gem 'font-awesome-sass'
gem 'devise'
gem 'roo'
group :development, :test do
gem "rspec-rails", "~> 2.0"
gem "factory_girl_rails", "~> 4.0"
gem "capybara"
gem "database_cleaner"
gem "selenium-webdriver"
end
student.csv: This is student test data.
enrollment_no,roll_no,address,father_name
11,21,test,test
17,21,test,test
18,21,test,test
19,21,test,test
20,21,test,test
22,21,test,test
23,21,test,test
24,21,test,test
source to share
First, after @SKV comment: the correct name for the CSV class is Roo::CSV
, not Roo::CSV
.
If the error persists, it means that any of the classes Roo
( Roo::CSV
, Roo::Excel
etc.) is not currently defined in the class Student
. This probably means that while you probably added the gem Roo
to yours Gemfile
and in the kit, you need require roo
to use the gem wherever you want. This is mentioned in the gem docs .
Add require 'roo'
to the top of the file student.rb
and it should work fine:
# student.rb
require 'roo' # <====
class Student < ActiveRecord::Base
def self.open_spreadsheet(file)
...
end
end
source to share
It's not Roo :: Csv, it's Roo :: CSV. Therefore, the code should like
def self.open_spreadsheet(file)
case File.extname(file.original_filename)
when ".csv" then Roo::CSV.new(file.path, nil, :ignore)
when ".xls" then Roo::Excel.new(file.path, nil, :ignore)
when ".xlsx" then Roo::Excelx.new(file.path, nil, :ignore)
else raise "Unknown file type: #{file.original_filename}"
end
end
btw, no need to do require 'roo'
source to share