我正在嘗試讀取一個ActiveStorage::Attached::One物件,該物件是用戶上傳的 txt 檔案。
誰能幫我為什么里面的變數“網格”line.chars.each_with_index是零?
txt_file = self.file.download.delete(' ') # returns the content of the file as a string
txt_file.each_line.with_index do |line, row_index|
next line if row_index == 0
if row_index == 1
grid = self.grids.create(generation: 0, rows: line[0].to_i, cols: line[1].to_i)
end
line.chars.each_with_index do |cell, column_index|
grid.cells.create(alive: cell == "*", row_position: row_index, column_position: column_index)
end
end
多謝
uj5u.com熱心網友回復:
無論何時row_index不等于0或等于1,grid都不會被賦值,因此計算結果為nil。換句話說,只要txt_fle有三行或更多,grid就會nil在某個點。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/471094.html
標籤:轨道上的红宝石 红宝石 rails-activestorage
上一篇:如何查詢JSONB列
