avillafiorita / jekyll-datapage_gen

Generate one page per yaml record in Jekyll sites.
371 stars 80 forks source link

error at blank record in data set: "no implicit conversion of nil into String (TypeError)" #118

Open mtlx03 opened 2 years ago

mtlx03 commented 2 years ago

Hi, your plugin is a lifesaver. I'm new to Jekyll and Ruby so please pardon my ignorance.

I'm getting this error, and failed output:

(eval):1:in `+': no implicit conversion of nil into String (TypeError)

when the script encounters a blank record (deliberate) in the csv data file:

biz | zip
ABC | 99835
XYZ | 99840
FGH | 99623
VBN |
ASD | 99833

config.yml looks like:

page_gen:
  - data: 'my-data'
    template: 'default'
    name_expr: record['biz'] + "_" + record['zip']
    title: 'biz'
    dir: 'misc'
    debug: yes

I'd like the filename to be biz and (optionally, only if it exists) _zip.

Here's more of the debug output:

debug (datapage-gen) Record read:
>> {"biz"=>"VBN", "zip"=>nil}       
debug (datapage-gen) Configuration variables:
>> index_files: false
>> dir: misc
>> page_data_prefix:
>> name:
>> name_expr: record['biz'] + "_" + record['zip']
>> title: zip
>> title_expr:
>> template: default
>> extension: html
(eval):1:in `+': no implicit conversion of nil into String (TypeError)

Many thanks for having a look!

avillafiorita commented 2 years ago

Hi, the issue is with the zip field, which is empty in the record before the last one: this causes name_expr to fail, since record['zip'] returns nil and nil cannot be concatenated to a string.

Try changing name_expr with something like:

 name_expr: "#{record['biz']}_#{record['zip']}"

or

 name_expr: (record['biz'] || "") + "_" + (record['zip'] || "")

Let me know if it works!