I'm trying to build a simple excel importer using RubyXL in rails to display barcode numbers. What i want to do is import am excel file and display the barcode results on an index page. I'm running into a couple errors and I'm not sure what exactly I'm missing. Here's what I have:
Controller:
class BarcodesController < ApplicationController
def index
@barcodes = Barcode.all
end
def show
@barcode = Barcode.find(params[:id])
end
def import
Barcode.import(params[:file])
redirect_to @barcode, notice: "Barcode imported"
end
end
Model:
class Barcode < ActiveRecord::Base
def self.import(file)
workbook = RubyXL::Parser.parse(params[:file].path)
worksheets = workbook.worksheets
puts "Found #{worksheets.count} worksheets"
worksheets.each do |worksheet|
puts "Reading: #{worksheet.sheet_name}"
num_rows = 0
worksheet.each do |row|
row_cells = row.cells.map{ |cell| cell.value }
num_rows += 1
end
puts "Read #{num_rows} rows"
end
end
end
View:
<h2>Import Barcodes</h2>
<%= form_tag import_barcodes_path do %>
<%= file_field_tag :file %>
<%= submit_tag "Import" %>
<% end %>
Routes:
Rails.application.routes.draw do
resources :users
resources :barcodes do
collection { post :import }
end
root to: "pages#root"
end
Here's the error message:
NameError in BarcodesController#import
undefined local variable or method `params' for Barcode(Table doesn't exist):Class
Extracted source (around line #4):
2
3
4
5
6
7
def self.import(file)
workbook = RubyXL::Parser.parse(params[:file].path)
worksheets = workbook.worksheets
puts "Found #{worksheets.count} worksheets"
Thank you for any feedback!