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 Viewing Todo Items: Part 1

Tomasz Jaśkiewicz
Tomasz Jaśkiewicz
5,399 Points

I got this error while running index_spec.rb test

I have this error:

F

Failures:

  1) Viewing todo items displays no items when a todo list is empty
     Failure/Error: within "#todo_list_#{todo_list.id}" do
     Capybara::ElementNotFound:
       Unable to find css "#todo_list_"
     # ./spec/features/todo_items/index_spec.rb:8:in `block (2 levels) in <top (required)>'

Finished in 0.05845 seconds
1 example, 1 failure

Failed examples:

rspec ./spec/features/todo_items/index_spec.rb:6 # Viewing todo items displays no items when a todo list is empty

Randomized with seed 1185

index_spec.rb:

require 'spec_helper'

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

    it "displays no items when a todo list is empty" do
        visit "/todo_lists"
        within "#todo_list_#{todo_list.id}" do
            click_link "List Items"
        end

        expect(page).to have_content("#TodoItems#index")
    end
end

index.html.erb:

<h1>Listing todo_lists</h1>

<table>
  <thead>
    <tr>
      <th>Title</th>
      <th>Description</th>
      <th colspan="3"></th>
    </tr>
  </thead>

  <tbody>
    <% @todo_lists.each do |todo_list| %>
      <tr id="<%= dom_id(todo_list) %>">
        <td><%= todo_list.title %></td>
        <td><%= todo_list.description %></td>
        <td>
          <%= link_to 'List Items', todo_list_todo_items_path(todo_list) %>
          <%= link_to 'Show', todo_list %>
          <%= link_to 'Edit', edit_todo_list_path(todo_list) %>
          <%= link_to 'Destroy', todo_list, method: :delete, data: { confirm: 'Are you sure?' } %>
        </td>
      </tr>
    <% end %>
  </tbody>
</table>

<br>

<%= link_to 'New Todo list', new_todo_list_path %>

What's wrong here? I have no idea

7 Answers

Tomasz Jaśkiewicz
Tomasz Jaśkiewicz
5,399 Points

Ok, I run some tests and I know what was wrong but please tell me why

This code is ok:

let!(:todo_list) { TodoList.create(title: "Groceries", description: "Grocery list.") }

and this is not:

let!(:todo_list) { TodoList.create(title: "Grocery list", description: "Groceries") }
Janis Celms
Janis Celms
44,170 Points

This helped me as well - only reason i can think of -

let!(:todo_list) { TodoList.create(title: "Groceries", description: "Grocery list.") } 

is mentioned in todo_lists specs - since i'm new to this i don't really understand the correlation.

Artem Prytkov
Artem Prytkov
11,932 Points

Check string <tr id="<%= dom_id(todo_list) %>"> when render your page. Is it really has css id you are searching for?

Tomasz Jaśkiewicz
Tomasz Jaśkiewicz
5,399 Points

Yes, it has. That's part of page source from localhost:3000

<tr id="todo_list_2">
        <td>Lista zakupów</td>
        <td>To co mamy kupić.</td>
        <td>
          <a href="/todo_lists/2/todo_items">List Items</a>
          <a href="/todo_lists/2">Show</a>
          <a href="/todo_lists/2/edit">Edit</a>
          <a data-confirm="Are you sure?" data-method="delete" href="/todo_lists/2" rel="nofollow">Destroy</a>
        </td>
      </tr>
Daniel Cardenas
PLUS
Daniel Cardenas
Courses Plus Student 9,236 Points

Hi Tomasz

I'm getting the same error but can't seem to solve it, I tried your code above, but no luck. Any thoughts?

Tomasz Jaśkiewicz
Tomasz Jaśkiewicz
5,399 Points

I detected this error by manipulating with other tests. Daniel, ensure that the object you're creating using let exists. In my case this object didn't exist.

I still have no idea why first let! is ok but second one is not.

Chris Vukin
Chris Vukin
17,787 Points

I am also getting this error and I have no idea why. It would be great if one of the mod's or the instructor could comment on this thread and explain this to us as we are paying for some interaction.

Chris Buczkowski
Chris Buczkowski
3,939 Points

I had the same error, but it was being caused by something else. I could not figure out what was causing it, so I copied your index_spec.rb code, commented out my own, and ran it, and it worked for me.

then I wanted to figure out why mine wasn't working, since it appeared to my eyes to match exactly, so I copied and pasted each line of your code above each line of mine (still commented out), put a couple of spaces before the uncommented code to get it to line up perfectly, and went through each line.

of course, it was almost immediately apparent what the problem was:

within "#todo_list_#{todo_list.id}" do <-your working code within "todo_list_#{todo_list.id}" do <-my failing code

one simple missing hashtag is all it takes to bring down otherwise good code, haha!

this to-do list module has me far more confused than any of the prior modules in any of the courses. it seems once I've slogged through this the first time, I should take the full Ruby course and then revisit. missing that sort of syntax error will be much easier then, I imagine.