无法使用 sqlite3 数据库将整个 csv 文件数据上传到 rails 4.2

Unable to upload whole csv file data into rails 4.2 with sqlite3 database

我的 csv 文件最多从大文件中提取 26 或 78 个值,并且不导入整个数据。

rails 控制台视图

irb(main):009:0> Stocking.count
   (0.0ms)  SELECT COUNT(*) FROM "stockings"
=> 26
irb(main):010:0> Stocking.delete_all
  SQL (109.4ms)  DELETE FROM "stockings"
=> 26
irb(main):011:0> Stocking.count
   (0.0ms)  SELECT COUNT(*) FROM "stockings"
=> 78
irb(main):012:0> Stocking.delete_all
  SQL (62.5ms)  DELETE FROM "stockings"
=> 78
irb(main):013:0> Stocking.count
   (0.0ms)  SELECT COUNT(*) FROM "stockings"
=> 26

当我使用 count 命令检查 rails 控制台时。

index.html.erb

<h2>Import Stock File</h2>
    <%= form_tag import_stockings_path, multipart: true do %>
    <%= file_field_tag :file %>
    <%= submit_tag "Import" %>
    <% end %>

stockings_controller.rb

class StockingsController < ApplicationController
    before_action :set_stocking, only: [:show, :edit, :update, :destroy]

  # GET /Stockings
  # GET /deldetails.json
  def index
    @stockings = Stocking.all
  end

  def import
    Stocking.import(params[:file])
    redirect_to stockings_url, notice: "Stockings imported."
  end

  # GET /Stockings/1
  # GET /Stockings/1.json
  def show
  end

  # GET /Stockings/new
  def new
    @stocking = Stocking.new
  end

  # GET /stockings/1/edit
  def edit
  end

  # POST /Stockings
  # POST /Stockings.json
  def create
    @stocking = Stocking.new(stocking_params)

    respond_to do |format|
      if @stocking.save
        format.html { redirect_to @stocking, notice: 'Stocking was successfully created.' }
        format.json { render :show, status: :created, location: @stocking }
      else
        format.html { render :new }
        format.json { render json: @stocking.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /Stockings/1
  # PATCH/PUT /stockings/1.json
  def update
    respond_to do |format|
      if @stocking.update(stocking_params)
        format.html { redirect_to @stocking, notice: 'Stocking was successfully updated.' }
        format.json { render :show, status: :ok, location: @stocking }
      else
        format.html { render :edit }
        format.json { render json: @stocking.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /stockings/1
  # DELETE /stockings/1.json
  def destroy
    @stocking.destroy
    respond_to do |format|
      format.html { redirect_to stockings_url, notice: 'Stocking was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_stocking
      @stocking = Stocking.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def stocking_params
      params.require(:stocking).permit(:season, :category, :articleno, :description, :color, :quantity, :rprice, :tamount, :cartonno )
    end
  end

stocking.rb

class 放养 < ActiveRecord::Base

def self.import(file)
        CSV.foreach(file.path, headers: true) do |row|
        puts row.inspect
        end
    end

end

application.rb

require File.expand_path('../boot', __FILE__)

require 'csv'

require 'rails/all'

# Require the gems listed in Gemfile, including any gems
# you've limited to :test, :development, or :production.
Bundler.require(*Rails.groups)

module Stock
  class Application < Rails::Application
    # Settings in config/environments/* take precedence over those specified here.
    # Application configuration should go into files in config/initializers
    # -- all .rb files in that directory are automatically loaded.

    # Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.
    # Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC.
    # config.time_zone = 'Central Time (US & Canada)'

    # The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
    # config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]
    # config.i18n.default_locale = :de

    # Do not swallow errors in after_commit/after_rollback callbacks.
    config.active_record.raise_in_transactional_callbacks = true


    config.middleware.use 'Utf8Sanitizer'

end
end

routes.rb

Rails.application.routes.draw do

  root 'stockings#index'

  resources :stockings do
    collection { post :import }
  end


end

我想将所有数据导入到我的 sqlite3 数据库中。

欢迎提出任何建议。

提前致谢。

真正的问题出在我上传的文件上,因为它的格式不正确,没有规范化。