How to fix: jekyll | Error: undefined method 'to_i' for #<Date:
After successfully installing the Jekyll Algolia Plugin (the gem jekyll-algolia
), I tried to push the content of my Jekyll website to an Algolia index. But when I run the jekyll algolia
command it throws the following error message:
$ bundle exec jekyll algolia Configuration file: /Users/arthur/sites/mywebsite/_config.yml Processing site... Extracting records... jekyll 3.6.2 | Error: undefined method `to_i' for #<Date: 2017-08-25 ((2457991j,0s,0n),+0s,2299161j)> Did you mean? to_time to_s
What causes this error
As with any error message, this message contains information about what causes the error. In my case, the problem is an incorrectly formatted date in the front matter of one of the posts. I learned this by searching 2017-08-25
(this date was indicated in the error message) among all the files of my website. Here’s what I saw:
---
title:
tagline:
ref:
date: 2017-08-25
layout:
permalink:
---
Note that in the example above, I have cleared the variables that are not relevant to the topic of this article.
I just forgot to specify the time in the date
variable… So, I know what caused the error.
You can learn more about the
date
variable in front matter of Jekyll website here.
How to solve it
To solve the problem, we need to set the date
variable correctly in front matter of each post on our website. In my case, I need to replace the date: 2017-08-25
with date: 2017-08-25 00:00:00
in front matter of one post. Just like that:
---
title:
tagline:
ref:
date: 2017-08-25 00:00:00
layout:
permalink:
---
Note that in the example above, I have cleared the variables that are not relevant to the topic of this article.
Let’s now try to run the jekyll algolia
command again:
bundle exec jekyll algolia
Configuration file: /Users/arthur/sites/mywebsite/_config.yml Processing site... Extracting records... Updating records in index spacexchimpcom... Records to delete: 0 Records to add: 625 ✔ Indexing complete
Note that in the example above I simplified the method call to
jekyll algolia
by using an alternative way of loading the API key to remove the need to add the API key in command.
And it worked.
Conclusion
That’s it, you’re done. So simple isn’t it?
If you are having trouble fixing this problem with the instructions above, but are being able to solve this problem with any another method please describe it in the comment section below. Thanks!
If this article has helped you solve the problem then please leave a comment
Thanks for reading!
Arthur is a designer and full stack software engineer. He is the founder of Space X-Chimp and the blog My Cyber Universe. His personal website can be found at arthurgareginyan.com.