我制作了一个新的控制器,试图为我的高级项目编写一个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预计页面将打开,不会在服务器上给出错误消息。我想从我一直在使用感应器、自动浇水和照明的植物中导入数据,试图将其与我通常使用的手工浇水和获得自然光的植物进行比较。
发布于 2019-04-04 16:08:26
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"])https://stackoverflow.com/questions/55520003
复制相似问题