2017-02-16 59 views
0

設置Apache服務器當我們選擇要導入我收到以下錯誤消息.csv文件後:導入.csv格式文件轉換成openproject

類型錯誤在ProductImportsController#create

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

你能分享回溯/日誌嗎? – user3402754

+0

TypeError(不支持將ActiveSupport :: HashWithIndifferentAccess隱式轉換爲String): app/controllers/product_imports_controller.rb:8:在'read'中 app/controllers/product_imports_controller.rb:8:在'create' – Ajay

回答

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