【问题标题】:How to Import an .csv format file into openproject如何将 .csv 格式文件导入 openproject
【发布时间】:2017-02-16 13:55:39
【问题描述】:

设置 apache 服务器后,当我们选择要导入的 .csv 文件时,我收到以下错误消息

NoMethodError in ProductImportsController#create 
undefined method `new' for :CSV:Symbol

我的 product_imports_controller.rb

class ProductImportsController < ApplicationController
  def new
    @product_import = ProductImport.new
  end

  def create
    @product_import = ProductImport.new(params[:product_import])
    if @product_import.save
      redirect_to root_url, notice: "Imported products successfully."
    else
      render :new
    end
  end
end

【问题讨论】:

  • 你要上传csv文件对吧?
  • 是的。现在我正在研究 csv

标签: ruby-on-rails ruby


【解决方案1】:
require 'csv'    
class ProductImportsController < ApplicationController
  def new
    @product_import = ProductImport.new
  end

  def create
    csv_text = File.read(params[:product_import])
    csv = CSV.parse(csv_text, :headers => true)
    csv.each do |row|
      ProductImport.create!(row.to_hash)
    end
  end
end

如需更多帮助,请阅读此内容..

https://ruby-doc.org/stdlib-2.0.0/libdoc/csv/rdoc/CSV.html

【讨论】:

  • 它在 ProductsController#import uninitialized constant Product::Csv 中抛出不同的错误 NameError
  • 您需要在哪里解析 CSV 'ProductImportsController' 或 'ProductsController'?
  • 我需要在 ProductImportsController 上解析文件
  • 我没有足够的徽章我是 ruby​​ on rails 的新手如果我分享你的 git 代码链接会有用!!!
  • 您可以在此处复制错误日志并粘贴
猜你喜欢
  • 1970-01-01
  • 2017-07-06
  • 2021-05-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多