Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/delphi/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 未定义的方法'fetch#u value';对于nil:使用Roo时使用NilClass_Ruby On Rails_Roo - Fatal编程技术网

Ruby on rails 未定义的方法'fetch#u value';对于nil:使用Roo时使用NilClass

Ruby on rails 未定义的方法'fetch#u value';对于nil:使用Roo时使用NilClass,ruby-on-rails,roo,Ruby On Rails,Roo,我正在尝试使用Roo将Excel电子表格中的数据导入Rails应用程序中的表(数据点) 我得到一个错误: undefined method `fetch_value' for nil:NilClass 该错误引用了第行的data_point.rb文件(完整代码摘录见下文): “应用程序跟踪”显示: 我对此感到困惑,因为我整个应用程序中的“全部查找”没有显示fetch_值的实例 以下是我的应用程序中的其他代码: 在我的模型中,data_point.rb: class DataPoint <

我正在尝试使用Roo将Excel电子表格中的数据导入Rails应用程序中的表(数据点)

我得到一个错误:

undefined method `fetch_value' for nil:NilClass
该错误引用了第行的data_point.rb文件(完整代码摘录见下文):

“应用程序跟踪”显示:

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

以下是我的应用程序中的其他代码:

在我的模型中,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
在我使用的Excel文件中,标题行的列名与上面提到的数据点的3个属性完全相同:年度收入、收入百分比、教育年限

另外,我已经看过RailsCast 396:导入CSV和Excel,并多次阅读了评论。我想我正在努力将示例代码翻译成Rails 4和/或我的个人属性分配(与RailsCast中使用的方法相比)


提前感谢您的帮助

正如我们在评论中发现的那样,您的非rails实践似乎有一些遗留问题。值得注意的是,覆盖的
initialize
方法以及每个属性的
attr\u访问器
。删除它们(并修复正确格式的
DataPoint.new()。我遇到的问题是,我的数据库中有一列被命名为“class”,这导致了各种各样的失败。我重新命名了遗留数据库中的列,一切正常


故事的寓意——检查列名中是否有保留字

这是昨天遇到的问题,原因是我定义了一个名为
class
的数据库字段,它是Ruby语言的保留字。它导致了冲突。

删除
def initialize()
方法

如果出于某种原因限制您删除
initialize
方法,而不是删除它,您可以尝试添加一个
super
调用,如下所示:

def initialize(annual_income, income_percentile, years_education)
  super()

  @annual_income = annual_income
  @income_percentile = income_percentile
  @years_education = years_education
end
这为我解决了问题

来源:

  • 可以找到有关
    super
    的更多信息

fetch\u value
是一种ActiveRecord方法。这是在执行
model.save时使用的东西之一fetch\u value
的是一个调用
\u read\u attribute
的方法,它执行
@attributes。fetch\u value
所以
@attributes
nil
。它可能是您的
数据点。新数据点(年收入、收入百分比、教育年限)
。尝试
DataPoint.new(年收入:年收入等)
谢谢,当我尝试data\u point=DataPoint.new(年收入:年收入,收入百分比:收入百分比,年教育:年教育)我得到错误的参数数(1代表3)。。。Rails将其理解为1而不是3个属性,这是否与我的语法有关?它将其解释为属性的散列,它应该这样做。您的
new
方法设置是否将每个属性作为参数,而不是
.new({attributes hash},{options hash})
?没关系,我看到你的initialize方法覆盖了它。好的,看看initialize方法在这里做什么,你正在覆盖它,所以它所做的一切都不再做了。(在示例之后,在注释之前单击view source)这是您的模型与我的模型之间的唯一区别,因此我尝试将其添加到我的一个属性中,并且<代码>无
。因为您正在连接到数据库,所以所有属性都已可访问。使属性可访问的任何内容都是要附加到该模型实例的额外数据,但不希望附加到数据库中。如果对这两个列使用相同的名称,这当然是一个问题。您找到解决方案了吗?或者刚刚重命名了该列?我重命名了该列。编辑原始答案,使其明确地反映我的遭遇。请添加解释,说明为什么这会解决问题,这将使您的答案更有帮助。
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
def import
    DataPoint.import(params[:file])
    redirect_to root_url, notice: "Data points imported."
end
def initialize(annual_income, income_percentile, years_education)
  super()

  @annual_income = annual_income
  @income_percentile = income_percentile
  @years_education = years_education
end