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 Adding Validations to Todo Items

Nicholas Cordoba
Nicholas Cordoba
4,998 Points

I am having the error Failure/Error: expect(page).to have_content("Content can't be blank.")

Failure/Error: expect(page).to have_content("Content can't be blank.") expected to find text "Content can't be blank." in "There was a problem adding that todo list item. 1 error prohibited this todo item from being saved: Content can't be blank Content" # ./spec/features/todo_items/create_spec.rb:32:in `block (2 levels) in <top (required)>'

It seems that It is reading the error and continuing the message instead of reseting and leaving only "Content can't be blank."

NEW.HTML.ERB

<%= 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 %>

CREATE_SPEC.RB

require 'spec_helper'

describe "Adding todo items" do let!(:todo_list) {TodoList.create(title: "Groceries", description: "Grocery List.")}

def visit_todo_list(list)
    visit "/todo_lists"
    within "#todo_list_#{todo_list.id}" do
        click_link "List Items"
    end
end

it "is successful with valid content" do 
    visit_todo_list(todo_list)
    click_link "New Todo Item"
    fill_in "Content", with: "Milk"
    click_button "Save"
    expect(page).to have_content("Added todo list item.")
    within("ul.todo_items") do
        expect(page).to have_content("Milk")
    end
end

it "displays and error with no content" do
    visit_todo_list(todo_list)
    click_link "New Todo Item"
    fill_in "Content", with: ""
    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 can't be blank.")
end

end

APPLICATION.HTML.ERB

<!DOCTYPE html> <html> <head> <title>Odot</title> <%= stylesheet_link_tag "application", media: "all", "data-turbolinks-track" => true %> <%= javascript_include_tag "application", "data-turbolinks-track" => true %> <%= csrf_meta_tags %> </head> <body>

<% flash.each do |type, message| %> <div class="alert flash <%= type %>"> <%= message%> </div> <% end %>

<%= yield %>

</body> </html>

I have tried to rewatch older videos to find my problem, but need fresh eyes. Thank you for the time and help.

2 Answers

ignacio palma
ignacio palma
8,698 Points

Yes i had the same error and it was syntax but in my case was the apostrophe "canΒ΄t instead of "can't". They might look the same in this page but they are not the same in the keyboard

I just had this same issue.

For expect(page).to have_content("Content can't be blank.") you have to remove the period at the end of "blank". Once the period is removed, leaving you with expect(page).to have_content("Content can't be blank"), the test will pass.

Looks like I'm about 6 months late on this answer, but hopefully this will help others moving forward.