0
我有一個簡單的CSV上傳器,下面是逐行並創建一個新的記錄(事件)。我還使用了unidecoder gem,並且想要在CSV上傳器創建的每個記錄中的字段(描述字段)上調用to_ascii
方法。這聽起來應該很簡單,但我不熟悉遍歷CSV文件。CSV上傳期間的運行方法
上傳者:
def self.import(file)
CSV.foreach(file.path, headers: true, encoding: "windows-1252:utf-8") do |row|
Event.create! row.to_hash
end
end
實現這個的正確方法:
def self.import(file)
CSV.foreach(file.path, headers: true, encoding: "windows-1252:utf-8") do |row|
description = row[2]
row[2] = description.to_ascii
Event.create! row.to_hash
end
end
謝謝!
奇數,它表示'to_ascii'不是一種方法。我從來沒有真正在模型中使用它,而是在控制器中使用它。我對Rails相當陌生,所以不知道如何繼續。有什麼想法嗎? – Devan
找不到'unidecoder'的寶石。什麼是錯誤輸出? – cortex
「NoMethodError at/events/import undefined method to_ascii for nil:NilClass」 這是Github for unidecoder gem:https://github.com/norman/unidecoder – Devan