我正在尝试使用 Rails 中的 RubyXL 构建一个简单的 excel 导入器来显示条形码编号。我想要做的是导入我的 excel 文件并在索引页面上显示条形码结果。我遇到了几个错误,我不确定我到底错过了什么。这是我所拥有的:
控制器:
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
模型:
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
看法:
<h2>Import Barcodes</h2>
<%= form_tag import_barcodes_path do %>
<%= file_field_tag :file %>
<%= submit_tag "Import" %>
<% end %>
路线:
Rails.application.routes.draw do
resources :users
resources :barcodes do
collection { post :import }
end
root to: "pages#root"
end
这是错误消息:
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"
感谢您的任何反馈!