Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Ruby Build a Todo List Application with Rails 4 Build a Todo List Application with Rails 4 Adding Validations to Todo Items

Errors persist in create_spec.rb

My code is exactly the same as in the video but I am still getting errors for 2 out of 3 tests.

The spec "is successful with valid content" passes but both the "no content" and "minimum content" spec fail.

rpsec seems to be having trouble finding both the "Content can't be blank" and the "Content is too short" messages.

I tried: 1) changing within("div.flash") to within("div.error_explaination") 2) removing the 'if' conditional from 'new.html.erb' 3) Downloading the project files and checking against the code contained but the project files often don't match the lesson exactly (which was the case with this lesson).

I suspect the error is in 'new.html.erb' but I can't figure out why My code is as follows:

<%= form_for [@todo_list, @todo_item] do |form| %>
  <% 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 |msg| %>
        <li><%= msg %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

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

  <%= form.submit "Save" %>
<% end %>

When I start the server to look at the application itself and do a manual test leaving one the description blank I get this error message:

2 errors prohibited this todo_list from being saved: Description translation missing: de.activerecord.errors.models.todo_list.attributes.description.blank Description translation missing: de.activerecord.errors.models.todo_list.attributes.description.too_short

I tried poking around on Google before I started a discussion, but no luck.

A lot of times I'm just missing a '.' or some other small errror, but I can't seem to find the error here. Anyone else encounter the same problem?

4 Answers

Artem Prytkov
Artem Prytkov
11,932 Points

He tells that "Description translation missing". It look like you set your default language as german

Look into your application.rb for line like

config.i18n.default_locale = :de

It`s is commented by default, my guess is that you uncommented it

Delete it and try again.

Adam Lewandowski
Adam Lewandowski
6,763 Points

Could the problem be with your create_spec.rb? Your code looks nearly the same as mine (that works).

My code in 'create_spec.rb' looks like this:

  it "displays an error with content less than 2 characters long" do
    visit_todo_list(todo_list)
    click_link "New Todo Item"
    fill_in "Content", with: "1"
    click_button "Save"
    within("div.flash") do
      expect(page).to have_content("There was a problem adding that todo list item.")
    end
    expect(page).to have_content("Content is too short")
  end

Everything (I believe) looks as it should.

Could it be a problem with 'todo_items_controller.rb'?

In the 'create' method there is an if/else statement controlling the flash message which looks like this:

  def create
    @todo_list = TodoList.find(params[:todo_list_id])
    @todo_item = @todo_list.todo_items.new(todo_item_params)
    if @todo_item.save
      flash[:success] = "Added todo list item."
      redirect_to todo_list_todo_items_path
    else
      flash[:error] = "There was a problem adding that todo list item."
      render action: :new
    end
  end

Inside the 'else' condition I see a flash error variable but nothing denoting whether it is blank or too short. Is putting another if/else condition to control the behavior for the "Content is too short/Content can't be blank" message incorrect?

Many thanks for this answer! That fixed it :)