使用 Roo 时 nil:NilClass 的未定义方法 `fetch_value' [英] undefined method `fetch_value' for nil:NilClass when using Roo

查看:72
本文介绍了使用 Roo 时 nil:NilClass 的未定义方法 `fetch_value'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 Roo 将数据从 Excel 电子表格导入到 Rails 应用中的表 (data_points) 中.

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

我收到错误:

undefined method `fetch_value' for nil:NilClass

并且该错误在行中引用了我的 data_point.rb 文件(请参阅下面的完整代码摘录):

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

data_point.save!

应用程序跟踪"说:

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'

我对此感到困惑,因为我整个应用程序中的查找全部"没有显示 fetch_value 的实例

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:

在我的模型中,data_point.rb:

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

在我的控制器中,除了标准的 rails 框架之外,我还添加了 data_points_controller.rb:

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

在我使用的 Excel 文件中,标题行的列名称与上面提到的 DataPoints 的 3 个属性完全相同:annual_income、income_percentile、years_education

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

附言我已经看过 RailsCast 396:导入 CSV 和 Excel,并多次阅读评论.我想我正在努力将示例代码转换为 Rails 4 和/或我对各个属性的分配(与 RailsCast 中使用的方法相比).

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

在此先感谢您的帮助!

推荐答案

正如我们在评论中发现的那样,您似乎有一些非 Rails 实践的遗留问题.值得注意的是,覆盖了每个属性的 initialize 方法和 attr_accessor.只需要移除它们(并修复 DataPoint.new() 以获得正确的格式).

It seems you had some leftovers from your non rails practice, as we found in the comments. Notably, the overwritten initialize method, and the attr_accessor for each of the attributes. Removing them (and fixing the DataPoint.new() for the correct format) was all that was needed.

这篇关于使用 Roo 时 nil:NilClass 的未定义方法 `fetch_value'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆