首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >红宝石rails索引法中的NameError未初始化常数

红宝石rails索引法中的NameError未初始化常数
EN

Stack Overflow用户
提问于 2019-04-04 15:41:17
回答 1查看 155关注 0票数 0

我制作了一个新的控制器,试图为我的高级项目编写一个web应用程序来处理.csv导入。我使用了示例代码和教程来实现这一点,当我运行它时,我将得到一个NameError,它出现在我的索引方法中,所以‘新’方法必须很好,因为它已经通过了那个方法。我的另一个控制器中的索引方法工作得很好,所以这个方法把我弄得很好。服务器上错误消息的图片我也尝试了大量的大写和下划线的组合,但是可能错过了正确的一个,我的控制器在下面。

代码语言:javascript
复制
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

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-04-04 16:08:26

代码语言:javascript
复制
class PlantDatum < ApplicationRecord

应该是

代码语言:javascript
复制
class PlantData < ApplicationRecord

在该模型中,在导入方法中修复这一行。

代码语言:javascript
复制
plant_data = Plant_data.where(id: plant_data_hash["id"])

长得像这样

代码语言:javascript
复制
plant_data = PlantData.where(id: plant_data_hash["id"])
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55520003

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档