Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
939 views
in Technique[技术] by (71.8m points)

ruby on rails - undefined method `fetch_value' for nil:NilClass when using Roo

I am trying to use Roo to import data from an Excel spreadsheet into a table (data_points) in a Rails app.

I am getting the error:

undefined method `fetch_value' for nil:NilClass

and that error references my data_point.rb file at line (see below for full code excerpt):

data_point.save!

The "Application Trace" says:

app/models/data_point.rb:29:in `block in import'
app/models/data_point.rb:19:in `import'
app/controllers/data_points_controller.rb:65:in `import'

I am puzzled by this because a "find all" in my entire app shows no instance of fetch_value

Here is the other code in my app:

In my model, data_point.rb:

class DataPoint < ActiveRecord::Base
attr_accessor :annual_income, :income_percentile, :years_education

def initialize(annual_income, income_percentile, years_education)
    @annual_income = annual_income
    @income_percentile = income_percentile
    @years_education = years_education
end

def self.import(file)
    spreadsheet = open_spreadsheet(file)
    header = spreadsheet.row(1)
    (2..11).each do |i| 
        annual_income = spreadsheet.cell(i, 'A')
        income_percentile = spreadsheet.cell(i, 'B')
        years_education = spreadsheet.cell(i, 'C')
        data_point = DataPoint.new(annual_income, income_percentile, years_education)
        data_point.save!
    end
end 

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

In my controller, data_points_controller.rb I have added, in addition to the standard rails framework:

def import
    DataPoint.import(params[:file])
    redirect_to root_url, notice: "Data points imported."
end

In the Excel file I'm using, the header row has column names that are exactly the same as the 3 attributes noted above for DataPoints: annual_income, income_percentile, years_education

P.s. I have already watched RailsCast 396: Importing CSV and Excel, and read the comments many times. I think I am struggling with translating the example code to Rails 4 and / or my assignment of individual attributes (vs. the approach used in the RailsCast).

Thanks in advance for any help!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I ran into a similar error also when trying to use Active Record with a legacy database. The problem for me was related to the fact that one of the columns of my database was named 'class,' which caused all sorts of things to fail. I renamed the column in the legacy database and everything worked fine.

Moral of the story- check the column names for any reserved words.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...