2017-02-16 57 views
0

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

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 
+0

你想上傳csv文件嗎? –

+0

是的。現在我正在處理csv – Ajay

回答

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

+0

它在ProductsController中引發了不同的錯誤NameError#import 未初始化的常量Product :: Csv – Ajay

+0

您需要解析CSV'ProductImportsController'或'ProductsController'嗎? –

+0

我需要解析ProductImportsController上的文件 – Ajay

相關問題