如何将.csv格式文件导入openproject

时间:2017-02-16 05:21:51

标签: ruby-on-rails ruby

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

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

我的product_imports_controller.rb

petName

1 个答案:

答案 0 :(得分:0)

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