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

alexandermuhr
alexandermuhr
3,466 Points

Error when running rspec on index_spec.rb - "Ambiguous match"

I am getting this error when running the test on index_spec.rb:

Failures:

  1) Viewing todo items displays no items when a todo list is empty
     Failure/Error: click_link "List Items"
     Capybara::Ambiguous:
       Ambiguous match, found 2 elements matching link "List Items"
     # ./spec/features/todo_items/index_spec.rb:9:in `block (3 levels) in <top (required)>'
     # ./spec/features/todo_items/index_spec.rb:8:in `block (2 levels) in <top (required)>'

Finished in 0.08274 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

index_spec.rb:

require 'spec_helper'

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

    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>
          <a href="">List Items</a>
          <%= 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 %>

routes.rb:

Rails.application.routes.draw do
  resources :todo_lists do
    resources :todo_items
  end
  root 'todo_lists#index'

3 Answers

Maciej Czuchnowski
Maciej Czuchnowski
36,441 Points

Remove this from index view:

<a href="">List Items</a>

Or make it a non-link, like:

<h2>List Items</h2>
alexandermuhr
alexandermuhr
3,466 Points

That worked!

Would you be able to tell me why that's the case?

Maciej Czuchnowski
Maciej Czuchnowski
36,441 Points

You had two links on the same page and they both said "List Items", one as anchor tag, one as Ruby-generated link tag:

<a href="">List Items</a>
<%= link_to "List Items", todo_list_todo_items_path(todo_list) %>

Imagine that you were told to "visit that page and click the List Items link". What would you do? Capybara got confused and reported that your request was ambiguous :)

Justin LeFurjah
Justin LeFurjah
12,347 Points

Thanks for the answer! Makes good sense. I am curious though, why does this not fail in the video? Jason does the same thing.

Asa Smith
Asa Smith
10,009 Points

I had the same problem. Same fix worked. I'd still like to know why Jason doesn't have the same issue.

Jason removes

<a href="">List Items</a>

at 7:12 in the video.