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

Andrés Fernández
Andrés Fernández
29,125 Points

Some help with RSpec syntax

Hello guys,

I'm now following along the ODOT application project. So far I understand pretty much everything, but I have a doubt about one line of syntax. Here's the code:

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

I don't understand where the css selector syntax comes from (the string passed into the #within method). For me It's clear the second part (with the curly braces) but I don't know where that first id came from (#todo_list_). Do the html.erb documents generated by rails have a default id so that they can be selected by other methods or that id is somewhere else in the app files??.

Maybe the question is too basic, but for me it's critical to close knowledge gaps before moving on.

Thanks a lot.

1 Answer

David Gross
David Gross
19,443 Points

When we created the let!(:todo_list) { TodoList.create(title: "Groceries", description: "Groecery list.") }
We created a todo_list in our database with an id. What the dom_id is telling rspec is we want that specific todo_list with the id of example: 2, so we can look at that todo_list.id(2) items.

<tr id="<%= dom_id(todo_list) %>">

The dom_id convention is to use the singular form of an object or class with the id following an underscore. This is just to be able to recognize an object with an id.

 within "#todo_list_#{todo_list.id}" do
      click_link "Edit" 
  end 

Since there are multiple "Edit" links. we are using the code to recognize a specific "Edit" link with the (:todo_list.id) you want to update. That way rspec knows which todo_list you want updated.

   Ambiguos match found # elements matching "Edit"

Without this code you will get this error as rspec does not know which Edit you want clicked.

Most of the time you can solve this with

    first(:link, 'Edit').click # new

But since we want to to target a specific id to update we are using the dom_id.