Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

undefined method `[]' for nil:NilClass when trying to display metadata in nanoc

Tags:

ruby

erb

nanoc

(Ruby noob) I'm using nanoc to generate a site. Here is my sample page with metadata:

--- 
title: abc
parameters:
    abc: def
    ghi: ijk
---

test

I know that I can have parameters one level up but I want to access them in layout file:

<%= @item[:parameters][:abc] %>

but I got following error:

undefined method `[]' for nil:NilClass

Parameters are passed to layout because when I do:

<%= YAML::dump(@item[:parameters]) %>

I can see them. As a Ruby noob I think there's a simple solution to my problem. Also, if you could post a snippet iterating through :parameters hash I would be grateful.

like image 872
keepkimi Avatar asked Dec 05 '25 06:12

keepkimi


1 Answers

You probably have more than one page on your site. Therefore parameters does not exist in the layout for every page nanoc processes. For some pages the element will be nil and hence the error you get.

Perhaps you need to rethink how you want to use those parameters, or then you need a conditional in your layout to look for parameters and only use them when they are present:

<% if @item[:parameters] %>
 <%= @item[:parameters][:abc] %>
<% end %>
like image 76
Casper Avatar answered Dec 07 '25 22:12

Casper



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!