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 Rails Routes and Resources A Route to an Index Action Populating the View

gene c
gene c
13,630 Points

how does the server know that |index| refers to numericals?

When the teacher adds index as a parameter, index automatically populates numbers from 0 to 4, but where does 0 to 4 come from?

1 Answer

Neil Northrop
Neil Northrop
5,095 Points

Hey gene c! I'll try to answer this for you but I wasn't 100% sure which part of the video your question was about. I don't have access to the whole video so I may have missed where your question came from.

I saw this bit of code in the text below the video:

<% 4.times do |index| %>
  <p><%= index %> fish</p>
<% end %>

If this bit of code is what your question was about or if this example is close to what your question is about, I should be able to answer.

The main part of this bit of code is centered around the times method. The times method allows you to do a loop a set number of times. So the code in the block portion gets executed X number of times but in this case, 4 times. The |index| portion of the code is a bit arbitrary as it could be named anything you want, just in this case it is named index. So index will refer to where in the loop the code is currently, 0, 1, 2, or 3. When you first start the loop, the code will begin with 0 then work its way to 3, executing the code inside the block a total of 4 times.

I hope that helps, if not please let me know!