-1
使用袋鼠寶石紅寶石.xlsx文件,我得到了以下錯誤:錯誤而進口在軌道上
NoMethodError in ClientsController#table
undefined method `phone' for nil:NilClass
這當我嘗試,.xls的或.csv文件導入到的.xlsx Ruby on Rails的應用程序中發生。下面
型號代碼給出:
attr_accessible :name, :kind, :address_attributes, :emails_attributes, :phones_attributes, :attachments_attributes
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]
client = find_by_id(row["id"]) || new
client.attributes = row.to_hash.slice(*accessible_attributes)
client.save!
end
end
def self.open_spreadsheet(file)
case File.extname(file.original_filename)
when '.csv' then Roo::Csv.new(file.path)
when '.xls' then Roo::Excel.new(file.path)
when '.xlsx' then Roo::Excelx.new(file.path)
else
raise "Unknown file type: #{file.original_filename}"
end
end
控制器代碼如下給出:
def import
Client.import(params[:file])
redirect_to clients_path, notice: "Clients imported."
end
應用/服務/配置/ application.rb中文件包括以下代碼:
require 'csv'
require 'iconv'
寶石文件包括以下內容:
gem 'roo', '~> 2.1.0'
gem "iconv", "~> 1.0.3"
gem 'roo-xls'
我申請你的建議@marwen。它不起作用: ClientsController中的RuntimeError#import 未知的文件類型:client1.xls – geethujoseph
您是否在使用'carrier_wave'進行文件上傳? – DevMarwen
是的,我正在使用carrier_wave進行文件上傳 – geethujoseph