前提・実現したいこと
タブ区切りCSVファイルからのインポートがうまくいかない。
CSVファイルをアップロードすると下記のエラーになる。
検索しても情報が少なく、自力で解決できそうにない。
発生している問題・エラーメッセージ
エラーメッセージ
uninitialized constant BtobQuotation::Csv
CSVファイルをアップロードした際のログ
I, [2019-07-17T18:26:15.712452 #9546] INFO -- : Started POST "/btob_quotations/import" for 192.168.11.30 at 2019-07-17 18:26:15 +0900 I, [2019-07-17T18:26:15.714922 #9546] INFO -- : Cannot render console from 192.168.11.30! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255 I, [2019-07-17T18:26:15.771705 #9546] INFO -- : Processing by BtobQuotationsController#import as HTML I, [2019-07-17T18:26:15.772242 #9546] INFO -- : Parameters: {"utf8"=>"✓", "authenticity_token"=>"7PtnpymZqYBn4UOP7OTDiVCi6ZhKZ0WZ8UhM8lzk/DPUqdXybcW6bN+tq/8AatqqK6yK+eUGrk8Qj20wKRd2jA==", "file"=>#<ActionDispatch::Http::UploadedFile:0x00007f433e909f10 @tempfile=#<Tempfile:/tmp/RackMultipart20190717-9546-1tgkswu.csv>, @original_filename="btob_quotations (2).csv", @content_type="application/vnd.ms-excel", @headers="Content-Disposition: form-data; name=\"file\"; filename=\"btob_quotations (2).csv\"\r\nContent-Type: application/vnd.ms-excel\r\n">, "commit"=>"オブジェクトを一括インポート"} D, [2019-07-17T18:26:15.774000 #9546] DEBUG -- : ActiveRecord::SessionStore::Session Load (0.4ms) SELECT `sessions`.* FROM `sessions` WHERE `sessions`.`session_id` = 'cf79cf20b72b18e45fb13cb190223dc2' ORDER BY `sessions`.`id` ASC LIMIT 1 I, [2019-07-17T18:26:15.786085 #9546] INFO -- : Completed 500 Internal Server Error in 13ms (ActiveRecord: 0.4ms) F, [2019-07-17T18:26:15.825356 #9546] FATAL -- : NameError - uninitialized constant BtobQuotation::Csv: app/models/btob_quotation.rb:34:in `open_spreadsheet' app/models/btob_quotation.rb:22:in `import' app/controllers/btob_quotations_controller.rb:86:in `import' I, [2019-07-17T18:26:15.928236 #9546] INFO -- : Started POST "/__better_errors/5de3351b5bd4fa51/variables" for 192.168.11.30 at 2019-07-17 18:26:15 +0900 I, [2019-07-17T18:26:15.929145 #9546] INFO -- : Cannot render console from 192.168.11.30! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255
該当のソースコード
btob_quotations\index.html.erb
<%= form_tag import_btob_quotations_path, multipart: true do %> <%= file_field_tag :file %> <%= submit_tag "オブジェクトを一括インポート" %> <% end %>
config/routes.rb
resources :btob_quotations do post :import, on: :collection end
config/application.rb
require 'csv' require 'iconv'
btob_quotations_controller.rb
def import BtobQuotation.import(params[:file]) redirect_to btob_quotations_url, notice: "レコードを追加しました" end
models\btob_quotation.rb
def self.import(file) spreadsheet = open_spreadsheet(file) header = spreadsheet.row(1) (2..spreadsheet.last_row).each do |i| row = Hash[[header, spreadsheet.row(i)].transpose] btob_quotation = find_by_id(row["id"]) || new btob_quotation.attributes = row.to_hash.slice(*accessible_attributes) btob_quotation.save! end end def self.open_spreadsheet(file) case File.extname(file.original_filename) when ".csv" then CSV.new(file.path, nil) else raise "Unknown file type: #{file.original_filename}" end end
参考にしているサイト
#396 Importing CSV and Excel – RailsCasts
補足情報(FW/ツールのバージョンなど)
Rails 5.0.7.2
Ubuntu VERSION="16.04.6 LTS (Xenial Xerus)"
ruby 2.4.6p354
gem 2.6.14.4
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/08/01 05:40
2019/08/01 06:56