名称错误 Ruby on rails 索引方法中的未初始化常量



我做了一个新的控制器来尝试处理我正在为我的高级项目编写的Web应用程序的.csv导入。我使用示例代码和教程来解决这个问题,当我运行它时我收到一个 NameError,它发生在我的索引方法中,所以"新"方法一定很好,因为它通过了那个方法。我另一个控制器中的索引方法工作正常,所以这个方法让我很困惑。服务器上错误消息的图片我还尝试了大量的大写字母和下划线组合,什么不是,但可能错过了正确的一个,我的控制器在下面

class PlantDataController < ApplicationController
  def new
    @plantdata = PlantData.new
  end
  def index
    @plantdata = PlantData.all
  end
  def import
    PlantData.import(params[:file])
    redirect_to root_url, notice: "Plant Data imported."
  end
end
class PlantDatum < ApplicationRecord
require 'csv'
  def self.import(file)
    CSV.foreach(file.path, headers: true) do |row|
      plant_data_hash = row.to_hash # exclude the price field
      plant_data = Plant_data.where(id: plant_data_hash["id"])
      if plant_data.count == 1
        plant_data.first.update_attributes(plant_data_hash)
      else
        Product.create!(plant_data_hash)
      end # end if !product.nil?
    end # end CSV.foreach
  end # end self.import(file)
end # end class

预计页面会打开,并且不会在我的服务器上给我错误消息。我想使用传感器和自动浇水和照明从我保存的植物中导入数据,以尝试将其与我通常使用手动浇水并获得自然光的植物进行比较。

class PlantDatum < ApplicationRecord

应该是

class PlantData < ApplicationRecord

在该模型中,在导入方法中修复此行

plant_data = Plant_data.where(id: plant_data_hash["id"])

看起来像这样

plant_data = PlantData.where(id: plant_data_hash["id"])

最新更新