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

Problem relating to error message, test at 3:00 in the video.

Really stuck here, please help.

ERROR MESSAGE

Failures: 1) Adding todo items displays an error with no content 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)>'

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 an 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

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

to_items_controller.rb

class TodoItemsController < ApplicationController def index @todo_list = TodoList.find(params[:todo_list_id]) end

def new @todo_list = TodoList.find(params[:todo_list_id]) @todo_item = @todo_list.todo_items.new end

def create @todo_list = TodoList.find(params[:todo_list_id]) @todo_item = @todo_list.todo_items.new(todo_items_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

private def todo_items_params params[:todo_item].permit(:content) end end

Any help is appreciated. I am pulling my hair out with this problem.

1 Answer

I was looking for an answer to this problem and found this old thread. I figured it out though and thought it best to share:

The code:

within "div.flash" do 
        expect(page).to have_content("There was a problem adding that todo list item.")
    end

Just drop the period so it says:

within "div.flash" do 
        expect(page).to have_content("There was a problem adding that todo list item")
    end

So there is no period after the word item

That worked for me as the text has to be exactly the same as the page. Example

Best option I'm finding is test the actual error and see what it says.

I hope the 6 months wait was worth it Nicolas Cordoba lol :)