Gem gem roo 插件使用问题报错

zhq_zhq · 2014年01月06日 · 最后由 lgn21st 回复于 2014年01月06日 · 3228 次阅读

def self.import_xls(file) spreadsheet = open_spreadsheet(file) header = spreadsheet.row(18) (19..spreadsheet.last_row).each do |i| row = Hash[[header, spreadsheet.row(i)].transpose] order = find_by_id(row["id"]) || new order.attributes = row.to_hash.slice(*accessible_attributes) order.save! end end

def self.open_spreadsheet(file) case File.extname(file.original_filename) when ".csv" then Roo::CSV.new(file.path, file.original_filename) when ".xls" then Roo::Excel.new(file.path, nil) when ".xlsx" then Roo::Excelx.new(file.path, nil) else raise "未知类型:#{file.original_filename}" end end

上传的无论是.csv 还是.xls 都是报这个错误

测试时报 uninitialized constant OpenofficeExcel 和 uninitialized constant Excelx

TypeError in OrdersController#import_xls

C:/Users/LHP/AppData/Local/Temp/RackMultipart20140106-4700-sarris is not an Excel file

Rails.root: F:/projects/quality_discount Application Trace | Framework Trace | Full Trace

app/models/order.rb:256:in new' app/models/order.rb:256:inopen_spreadsheet' app/models/order.rb:243:in import_xls' app/controllers/orders_controller.rb:55:inimport_xls'

Request

Parameters:

{"utf8"=>"✓", "authenticity_token"=>"Io2Iq3bsBE4XNuTdcBXFwmD/8rWN32tJZ8OI/ZQNYk8=", "file"=>#>, "commit"=>"导入"}

这个 gem 需要用到其他的 gem 吧,RTFM

要学会用 markdown 格式化代码,否则可读性实在太差,会影响其他人帮你解决问题的兴趣。

需要 登录 后方可回复, 如果你还没有账号请 注册新账号