Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Ruby Build a Todo List Application with Rails 4 Build a Todo List Application with Rails 4 Cleaning Up Our View

0 errors prohibited this todo_item from being saved:

Why am I getting this red error message when I click "New Todo Item" in my app? I've followed right up to the end of this video, and everything seems to work fine. The item is also actually created and saved.

...So what's with the error message?

Many thanks for any clues!

Maciej Czuchnowski
Maciej Czuchnowski
36,441 Points

You'd have to show us all your code (preferably github) so that we could simulate this on our own machines.

Thanks for the swift response Maciej!

Problem solved. I was missing the first line:

"<% if @todo_item.errors.any? %>"

from my _form.html.erb

Too much late night coding!

Many thanks anyway!

This is funny/odd, but I was having the same issue, and I think I remember deleting those lines in order to make the file "match" what was on screen at one point. I bet you did that too--and it messed us both up! Perhaps a janky video is to blame! :grinning:

1 Answer

Jonathan Chua
Jonathan Chua
4,136 Points

If anyone else is encountering this problem, it is because of a discrepancy in the video where Jason separates the code that displays error messages into a form to keep us from repeating ourselves. The code is extracted from the edit.html.erb and new.html.erb files in the app/views/todo_items folder. When Jason initially had us write the code in the edit and new files, the error_explanation div was wrapped in an if statement. In the video that shows him extracting that code out to a form, the if statement is inexplicably absent. The if statement hides the error message div if there are no errors. If you match your code with what you see in the video, the error message will always display in your todo list even if there are zero errors.

Here is what the code in the _form.html.erb file should look like. Notice the if statement that wraps the error_explanation div.

<% if @todo_item.errors.any? %>
    <div id="error_explanation">
    <h2><%= pluralize(@todo_item.errors.count, "error") %> prohibited this todo item from being edited:</h2>

      <ul>
      <% @todo_item.errors.full_messages.each do |message| %>
        <li><%= message %></li>
      <% end %>
      </ul>
    </div>
<% end %>


    <%= form.label :content %>
    <%= form.text_field :content %>
    <%= form.submit "Save" %>