ruby on rails - undefined method `fetch_value' for nil:NilClass when using Roo -
i trying use roo import data excel spreadsheet table (data_points) in rails app.
i getting error:
undefined method `fetch_value' nil:nilclass
and error references data_point.rb file @ line (see below 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 puzzled because "find all" in entire app shows no instance of fetch_value
here other code in app:
in 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 |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" roo::excelx.new(file.path) else raise "unknown file type: #{file.original_filename}" end end end
in controller, data_points_controller.rb have added, in addition standard rails framework:
def import datapoint.import(params[:file]) redirect_to root_url, notice: "data points imported." end
in excel file i'm using, header row has column names same 3 attributes noted above datapoints: annual_income, income_percentile, years_education
p.s. have watched railscast 396: importing csv , excel, , read comments many times. think struggling translating example code rails 4 , / or assignment of individual attributes (vs. approach used in railscast).
thanks in advance help!
it seems had leftovers non rails practice, found in comments. notably, overwritten initialize
method, , attr_accessor
each of attributes. removing them (and fixing datapoint.new()
correct format) needed.
Comments
Post a Comment