Rails Csv.new从哪个类调用?

时间:2014-02-27 19:05:10

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

我在这里创建我自己的Rayan Bates CSV截屏版本......

http://railscasts.com/episodes/396-importing-csv-and-excel

我的模特中有这个......

 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

...我的应用程序中出现此错误...

 NameError in StudentsController#import

uninitialized constant Student::Csv

Rails.root: /home/wintas/railsApps/t4
Application Trace | Framework Trace | Full Trace

app/models/student.rb:25:in `open_spreadsheet'
app/models/student.rb:13:in `import'
app/controllers/students_controller.rb:12:in `import'

我无法找到类'Csv'的初始化位置,或者应该来自何处。任何帮助表示赞赏。

4 个答案:

答案 0 :(得分:2)

我相信自从Railscast发布以来,Roo已更新为Csv名称空间下的名称空间ExcelExcelxRoo。试试这个:

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

答案 1 :(得分:2)

使用roo 1.13.2的那个函数的新等价物是:

def self.open_spreadsheet(file)
  case File.extname(file.original_filename)
    when ".csv" then Roo::CSV.new(file.path, file_warning: :ignore)
    when ".xls" then Roo::Excel.new(file.path, file_warning: :ignore)
    when ".xlsx" then Roo::Excelx.new(file.path, file_warning: :ignore)
    else raise "Unknown file type: #{file.original_filename}"
  end
end

答案 2 :(得分:0)

在ruby标准库中,csv全部为大写:CSV。这有帮助吗?

答案 3 :(得分:0)

尝试重启服务器; 之前我遇到了问题并重新启动服务器解决了问题