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 Todo Items

Master Mind
Master Mind
4,826 Points

Flash Message In Application Still Gets Error

Adding todo items is successful with valid content (FAILED - 1)

Failures:

1) Adding todo items is successful with valid content Failure/Error: expect(page).to have_content("Added todo list item.") expected to find text "Added todo list item." in "Added todo list item Grocery List Milk New Todo Item" # ./spec/features/todo_items/create_spec.rb:18:in `block (2 levels) in <top (required)>'

Finished in 0.2739 seconds 1 example, 1 failure

Failed examples:

rspec ./spec/features/todo_items/create_spec.rb:13 # Adding todo items is successful with valid content

Randomized with seed 53759

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

3 Answers

Hiya,

I'm not sure the code you posted is anything to do with your error.

The test is failing but I'm also not sure it is testing the right thing. The test should look something like:

    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("td.todo_items") do
            expect(page).to have_content("Milk")
        end
    end

The output your page is creating doesn't quite match that.

Your todo_items_controller.rb should deal with most of that:

  def create

    @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

How do your files look?

Steve.

Master Mind
Master Mind
4,826 Points
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_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

  private
  def todo_item_params
    params[:todo_item].permit(:content)
  end
end
Master Mind
Master Mind
4,826 Points

I had to delete my application.html.erb file and make a new one. Then it passed.

Ah yes - you'd lost the whole body/nav element out of that! Oopsie!!

Master Mind
Master Mind
4,826 Points
require 'spec_helper'

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

  def visit_todo_list(list)
    visit "/todo_lists"
    within "#todo_list_#{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

end

OK,

The test was expect(page).to have_content("Added todo list item.") expected to find text "Added todo list item." which looks fine. But the result was "Added todo list item Grocery List Milk New Todo Item".

I'm not sure where that's occurring. Do you have this in GitHub?

Master Mind
Master Mind
4,826 Points

My application page was acting a little funky, so I had to delete it and create a new one. Don't know why. The test passes now though.

Glad you got it fixed! :-)