【问题标题】:Getting uninitialized constant Student::Roo error while importing csv file in rails在rails中导入csv文件时出现未初始化的常量Student :: Roo错误
【发布时间】:2015-07-08 10:43:01
【问题描述】:

我有一个应用程序,我想在该应用程序上提供从 CSV 和 Excel 文件格式导入记录的功能。我正在使用roo gem,但在导入时会出现错误“未初始化的常量 Student::Roo”。

代码如下:

学生.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 %>   

宝石文件:

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 : 这是学生的测试数据。

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

【问题讨论】:

    标签: ruby-on-rails-4 roo-gem


    【解决方案1】:

    不是 Roo::Csv,而是 Roo::CSV。所以代码应该像

    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
    

    顺便说一句,不用require 'roo'

    【讨论】:

    • 我仍然收到错误“未初始化的常量 Roo”
    • 这不是问题。如果定义了::Roo 类,它将在Student 范围内找到。如果未找到,则 rails 会查找 Student::Roo,如果也未找到,则会失败。
    • 亲爱的@dgilperez,请在投反对票和发表评论之前检查一下。不会有定义的类 ::Roo,它是在全局范围内定义的。问题其实是,应该是 Roo::CSV,而不是 Roo::Csv
    • @SKR 这不是你的回答所说的。你可以在没有:: 的情况下调用Roo(命名空间模块),如果需要,它会在文件顶部找到。问题不在于在类实例化中添加或不添加::。这就是我投反对票的原因。
    【解决方案2】:

    首先,在@SKV 注释之后:CSV 类的正确名称是Roo::CSV 而不是Roo::Csv

    如果错误仍然存​​在,则表示此时在Student 类中未定义任何Roo 类(Roo::CSVRoo::Excel 等)。这可能意味着,虽然您可能将 roo gem 添加到您的 Gemfile 并捆绑在一起,但您需要在要使用 gem 的任何地方添加 require roogem docs中提到了这一点。

    require 'roo' 添加到student.rb 文件的顶部,它应该可以正常工作:

    # student.rb
    require 'roo'  # <==== 
    
    class Student < ActiveRecord::Base
      def self.open_spreadsheet(file)
        ...
      end
    end
    

    【讨论】:

    • 在 student.rb 文件上使用 require 'roo' 后,我收到错误“无法加载此类文件 -- roo”
    • 你能给我们看看你的 Gemfile 吗?您是否运行了bundle install 并重新启动了服务器?
    • 我不知道在您的情况下哪个 gem 或代码需要 iconv,但是将其添加到您的 gemfile 中:gem "iconv" 然后运行 ​​bundle install 并重新启动
    • 另外,您使用的是哪个版本的roo?这显示在您的捆绑安装控制台输出中。如果不是最新版本,运行bundle update roo并重启。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多