Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby - Error: undefined method `[]' for nil:NilClass

Tags:

null

ruby

jekyll

So, I'm new to Ruby and I'm trying to install this photo gallery to my Jekyll blog. However, when I'm trying to run

jekyll build

I get this error message:

Liquid Exception: undefined method `[]' for nil:NilClass in photography/index.html 
jekyll 3.7.2 | Error:  undefined method `[]' for nil:NilClass

With --trace it points me to:

/Users/hal9000/Desktop/Plommonstop/_plugins/jekyll-exiftag-mod.rb:18:in `render': undefined method `[]' for nil:NilClass (NoMethodError)

But now I don't understand how to proceed. jekyll-exiftag-mod looks like this:

require 'exifr/jpeg'

#Based on https://github.com/benib/jekyll-exiftag by Beni Buess (MIT License)
#Edited to work as a Liquid-Block instead of a Liquid-Tag, reads the filename from between the
#brackets. --T.Winter

module Jekyll
  class ExifTag < Liquid::Block

    def initialize(tag_name, params, token)
      super
      @args = self.split_params(params)
    end

    def render(context)
      #abort context.registers[:site].config['source'].inspect
      sources = Array.new(context.registers[:site].config['exiftag']['sources'])
      # first param is the exif tag
      tag = @args[0]

      # if a second parameter is passed, use it as a possible img source
      if @args.count > 1
        sources.unshift(@args[1])
      end

      # the image can be passed as the third parameter
      img = super

      # first check if the given img is already the path
      if File.exist?(img)
        file_name = img
      else
      # then start testing with the sources from _config.yml
        begin
          source = sources.shift
          file_name = File.join(context.registers[:site].config['source'], source, img)
        end until File.exist?(file_name) or sources.count == 0
      end
      # try it and return empty string on failure
      begin
        exif = EXIFR::JPEG::new(file_name)
        return tag.split('.').inject(exif){|o,m| o.send(m)}
      rescue
        "ERROR, EXIF-Tag RB"
      end
    end

    def split_params(params)
      params.split(",").map(&:strip)
    end
  end
end

Liquid::Template.register_tag('exiftag', Jekyll::ExifTag)

With row 18:

sources = Array.new(context.registers[:site].config['exiftag']['sources'])

What does it mean with "Undefined method `[]' for nil:NilClass?" And what exactly is making this problem occur?


1 Answers

The error could simply mean that you have not set ['exiftag']['sources'] in your config file.

Your config file should have something similar to below (entry1 and entry2 are just examples):

exiftag:
  sources:
    - entry1
    - entry2

Do note, indentation is important in YAML as well..

like image 130
ashmaroli Avatar answered Nov 09 '25 08:11

ashmaroli



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!