Adding CSV Import to a ROR application

前端 未结 1 746
一个人的身影
一个人的身影 2021-01-17 03:29

I am adding a CSV import for bulk orders in my online ordering application. Basically what I am looking to do is to create a cart out of the CSV data.

I created an i

1条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-17 04:00

    Your form is submitting to the csv_import action. Your controller defines a csv_import= method. Not sure what else is in your controller or what that csv_import= method is for, but you need a csv_import method in your controller that can process the request.

    Something like this perhaps:

    def csv_import
    
      num_imported = 0
      num_failed = 0
    
      CSV.foreach(params[:file].tempfile) do |row|
        c = CsvImport.new(
          cart_items: row[1],
          cart_items_quantity: row[2],
          cart_items_price: row[3],
          cart_items_description: row[4],
          cart_items_upc: row[5],
          cart_items_sku: row[6]
        )
        if c.save
          num_imported += 1
        else
          num_failed += 1
        end
      end
    
      flash.now[:message] = "CSV Import Successful, #{num_imported} new records added to data base. #{num_failed} failed to import."
    end
    

    0 讨论(0)
提交回复
热议问题