gpt4 book ai didi

ruby - 错误 : undefined method `[]' for nil:NilClass

转载 作者:行者123 更新时间:2023-12-05 06:37:05 25 4
gpt4 key购买 nike

所以,我是 Ruby 的新手,我正在尝试安装 this photo gallery到我的 Jekyll 博客。但是,当我尝试运行时

jekyll build

我收到此错误消息:

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

使用 --trace 它指向我:

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

但现在我不知道如何进行。 jekyll-exiftag-mod 看起来像这样:

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)

第 18 行:

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

“nil:NilClass 的未定义方法‘[]’”是什么意思?究竟是什么导致了这个问题的发生?

最佳答案

该错误可能只是意味着您没有在配置文件中设置 ['exiftag']['sources']

你的配置文件应该有类似于下面的内容(entry1 和 entry2 只是例子):

exiftag:
sources:
- entry1
- entry2

请注意,缩进在 YAML 中也很重要..

关于 ruby - 错误 : undefined method `[]' for nil:NilClass,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48527061/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com