5

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.

2
  • You are getting nil value in the contents thats why you are getting the error. Once you check the data for the contents. Dec 12, 2012 at 8:04
  • Once I check the data for the contents? How do I get my contents to appear in editor-made websites?
    – alt
    Dec 12, 2012 at 8:14

1 Answer 1

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.