设置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
答案 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