Ruby on rails 在Rails中导入Excel文件-未初始化:Excel

Ruby on rails 在Rails中导入Excel文件-未初始化:Excel,ruby-on-rails,ruby,import-from-excel,Ruby On Rails,Ruby,Import From Excel,这是我的应用程序跟踪(不确定标准是否像代码一样缩进): 我在看Railscast第396集的大纲 我的代码本质上是相同的,但我得到了这个错误。我假设gem'roo'已经改变了。无论如何,我会在下面发布一些代码 models/hospital.rb class Hospital < ActiveRecord::Base has_one :ctscan has_one :mri has_one :hipreplacement has_one :kneereplacement

这是我的应用程序跟踪(不确定标准是否像代码一样缩进):

我在看Railscast第396集的大纲

我的代码本质上是相同的,但我得到了这个错误。我假设gem'roo'已经改变了。无论如何,我会在下面发布一些代码

models/hospital.rb

class Hospital < ActiveRecord::Base
  has_one :ctscan
  has_one :mri
  has_one :hipreplacement
  has_one :kneereplacement
  has_one :kneearthroscopy
  has_one :shouldersurgery

  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]]
        hospital = find_by_id(row["id"]) || new 
        hospital.attributes = row.to_has.slice(:id, :name, :address)
        hospital.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
class医院
医院控制器.rb

class HospitalsController < ApplicationController
  def index
    @search = Hospital.search(params[:q])
    @hospitals=@search.result
  end
  def import
    Hospital.import(params[:file])
    redirect_to root_url, notice: "Products Imported"
  end
end
class HospitalsController
config/application.rb(*注意我在这两个方面都需要,因为我不知道应该在哪里指定它)

需要文件。展开路径('../boot',文件)
需要“rails/all”
需要“rubygems”
需要“roo”
需要“csv”
需要“iconv”
Roo::Excel.new
Bundler.require(:default,Rails.env)
医学选择模块
类应用程序
你试过换衣服了吗

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
致:


我想我也曾经面对过这个问题。用这个解决方案结束了这个案例。

我的config/application.rb文件到处都是。我不确定是否需要Roo::Excel.new行,我也希望能得到一些帮助。你也可以将错误的堆栈跟踪添加到帖子中吗?@Suraj你使用的是哪个版本的
Roo
?这是我这边的一些错误,经过一些调整后仍然有效,请告诉我这些调整
require File.expand_path('../boot', __FILE__)

require 'rails/all'
require 'rubygems'
require 'roo'
require 'csv'
require 'iconv'
Roo::Excel.new

Bundler.require(:default, Rails.env)

module MedicalChoice
  class Application < Rails::Application
    require 'rails/all'
    Roo::Excel.new
    require 'rubygems'
    require 'roo'
    require 'csv'
    require 'iconv'

  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
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