Search code examples
ruby-on-railsrubyjekyllstoryblok

STORYBLOK, RUBY: undefined method `[]' for nil:NilClass (NoMethodError)


I don't know that much about RUBY, just thought that you guys might help me with this. I'm using Storyblok as my headless CMS and JEKYLL when I'm building serve it this is the error that I got;

33: from C:/project/test/_plugins/storyblok_generator.rb:8:in `generate'
32: from C:/project/test/_plugins/storyblok_cms/generator.rb:12:in `generate!'
C:/project/test/vendor/cache/ruby/2.7.0/gems/storyblok-3.0.1/lib/storyblok/client.rb:354:in `block (2 levels) in find_and_fill_links': undefined method `[]' for nil:NilClass (NoMethodError)

the code below is from _plugins/storyblok_cms/generator.rb

def generate!
  timestamp = Time.now.to_i
  links = client.links(cv: timestamp)['data']['links']
  stories = client.stories(per_page: 100, page: 1, cv: timestamp)['data']['stories']   #line 12

  stories.each do |story|
    # create all pages except global (header,footer,etc.)
    content_type = story['content']['component']
    if content_type != 'shared'
      site.pages << create_page(site, story, links)
    end
    rescue UnknownContentTypeError => e
    # for production, raise unknown content type error;
    # for preview and other environments, issue an warning only since the content_type might be available
    # but the code handling that content type might be in a different branch.
    Jekyll.env == 'production' ? raise :  Jekyll.logger.warn(e.message)
  end

  site.data['stories'] = stories
  site.data['articles'] = stories.select { |story| story['full_slug'].start_with?('articles') }
  site.data['shared'] = stories.select { |story| story['full_slug'].start_with?('shared') }
end

the code below is from _plugins/storyblok_generator.rb

require "storyblok"

module Jekyll
  class StoryblokGenerator < Jekyll::Generator
    safe true

    def generate(site)
      StoryblokCms::Generator.new(site).generate!   #line 8
    end
  end
end

Additional Info:

  • ruby version: ruby 2.7.4p191 (2021-07-07 revision a21a3b7d23) [x64-mingw32]
  • jekyll version: jekyll 4.2.1
  • OS: Windows 10

Solution

  • I've actually found the solution to this, So this error occurs because I have created a page template in Block Library in Storyblok and then firing bundle exec jekyll serve command in terminal without creating the page template source/file in my project directory.

    So I have an about_us block (content-type) in Block Library created and then when I fire bundle exec jekyll serve without creating an about_us.html first in _layouts folder, It would trigger the error.

    Solution;
    Make sure to create the source/file first in the _layouts folder if you have created a block (content-type) in block library.