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