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

Jacob Horn
Jacob Horn
9,255 Points

"0 errors prohibited this todo_item from being saved" message is now showing each time I edit a todo item.

I don't believe this was an issue before the render partial: form setup from this video. Here is my code for both files:

edit.html
<h1><%= @todo_list.title %> - Editing Todo List Item </h1>

<%= form_for [@todo_list, @todo_item] do |form| %>

<%= render partial: form %>

<% end %>
_form.html
  <div id="error_explanation">
      <h2><%= pluralize(@todo_item.errors.count, "error") %> prohibited this todo_item from being saved:</h2>

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


  <%= form.label :content %>
  <%= form.text_field :content %>

  <%= form.submit "Save" %>

Thanks in advance for your help!

1 Answer

Hi Jacob,

I think that there was a mistake when this video was edited after recording. The "error_explanation" div should be wrapped in an if block so that it is only output to the page if there are some errors. In your case it gets output even when there are zero errors which is why you see that message.

If you pause the video at 2:41 you'll see the code that you have but if you pause it again at 3:14 you'll see the additional code. It only shows for 1 second. It must have been added but not shown in the video.

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

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