Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Locomotive Editor throws 'NoMethodError' when trying to loop over a model's entries

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.

like image 408
alt Avatar asked Nov 03 '22 09:11

alt


1 Answers

  1. Maybe the loop is acting as though there's no data because the backend Rails is connecting to doesn't have data.

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

  1. 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.

  2. 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".

like image 147
inquiryqueue Avatar answered Nov 12 '22 17:11

inquiryqueue