Skip to content

fix: raise a clear error on blank CSV column headers (#197)#224

Merged
Fivell merged 2 commits into
masterfrom
fix/blank-csv-header-error
Jun 15, 2026
Merged

fix: raise a clear error on blank CSV column headers (#197)#224
Fivell merged 2 commits into
masterfrom
fix/blank-csv-header-error

Conversation

@Fivell

@Fivell Fivell commented Jun 15, 2026

Copy link
Copy Markdown
Member

Closes #197.

Problem

A CSV whose header row contains an empty cell — e.g. a stray leading comma from a spreadsheet export:

,Name,Last name,Birthday
x,John,Doe,1986-05-01

parses the first header as nil, and prepare_headers crashed on nil.underscore:

Error: undefined method 'underscore' for nil

The fix suggested in the issue (coercing headers with to_s) does not resolve it — it only moves the failure to import time, because an unnamed "" column maps to no attribute:

Error: Missing column for value <> at index 0

Fix

Detect blank header cells up front and raise an ActiveAdminImport::Exception naming the offending column (1‑based). That exception is already rescued by the import action, so the user sees an actionable flash instead of a NoMethodError:

Error: blank column header at column 1

This is the option B behaviour (fail with a clear message) rather than silently dropping unnamed columns — a stray comma shouldn't quietly discard data.

Tests

New with a blank column header context: uploads a fixture with an empty leading header, asserts the clear error, that no NoMethodError leaks, and that nothing is imported.

Full suite green locally on Ruby 3.3 / Rails 8.1.3 / AA 3.5.1 / SQLite — 62 examples, 0 failures.

Fivell added 2 commits June 15, 2026 12:18
A CSV with an empty header cell (e.g. a leading comma) produced a nil header,
and prepare_headers crashed with `undefined method 'underscore' for nil`.
Merely coercing the header to a string only moves the failure to import time
(`Missing column for value <> at index 0`), since an unnamed column maps to no
attribute. Detect blank headers up front and raise an ActiveAdminImport::Exception
naming the offending column, so the user gets an actionable flash instead of a
NoMethodError.
Generalize the blank-header spec into a shared example and exercise a blank
column at the beginning (col 1), the middle (col 2) and the end (col 4),
asserting the reported column position for each.
@Fivell Fivell merged commit 72ed02b into master Jun 15, 2026
38 checks passed
@Fivell Fivell mentioned this pull request Jun 15, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Error if csv file consists empty columns

1 participant