将.csv格式文件导入openproject

时间:2017-02-16 07:42:53

标签: ruby-on-rails ruby

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

ProductImportsController#create中的TypeError:

no implicit conversion of ActiveSupport::HashWithIndifferentAccess into String

我的product_imports_controller.rb

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

1 个答案:

答案 0 :(得分:0)

试试这个: 问题是控制器中存在哈希的安全方面。 你试试row.to_hash.permit!,但我怀疑它会起作用

在模型中创建它:

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_from_csv(row.to_hash)
    end
  end

end


class ProductImport < ActiveRecord::Base
  def self.create_from_csv(data)
     self.create!(data)
  end   
end