So I'm using Locomotive, the Ruby on Rails CMS, and its editor to create a blog. In the Locomotive backend I made a model with the slug 'posts' containing a could blog posts. Then, in my index.liquid file I'm trying to loop over the contents of the model and display the entires.
Here's the liquid loop:
{% for post in contents.posts %}
{{ post.title }}
{% endfor %}
But it shows me 'NoMethod Error at /', 'undefined method contents
for nil:NilClass'. If I remove the .posts from the for...in loop there's no error but the loop doesn't even happen once, naturally, since only contents
can't be looped over.
I've read the documentation and the code should work. Why does it act as if there's no data?
Update Dec 11th, 2012: If I push my code to the real locomotive engine it works perfectly, only the editor throws this error. I use the editor so I can edit the code through my IDE and not in the browser.
Update Dec 12th, 2012: Figured out this isn't what the Locomotive Editor was intended for. You're supposed to use a dummy database while making the website then push it to the engine to use it with your own database.
If your content is in the production database, Rails needs to be using the production database, too.
rails server -e production
See further notes on switching to the production environment in this question: Change a Rails application to production
If "contents" is nil, then accessor methods like ".posts" will show up as no method errors. Try adding a section to your template so you can handle things if "contents.nil?" is true.
Since I'm not familiar with Locomotive CMS, just Rails, here's another thing to try in situations like this: Be careful with singular and plural references to your models! Rails is very particular about these names, you might need to use "content" rather than "contents".
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With