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
jadaml
11,593 PointsLoop through array in an ERB template
Q: Loop through our statuses array in an ERB template. Assume we have statuses set up as an array and want to print out the name.
I am not quite sure what it is they are asking of me here.
<% @statuses.each do |status| %>
Name: <br />
<% end %>
I got the answer correct, but I just don't understand it.
<% @statuses.each do |status| %>
Name: <br />
<tr>
<td><%= status.name %></td>
</td>
<% end %>
Build a simple ruby app/front end development/code challenge 2 http://teamtreehouse.com/library/programming/build-a-simple-ruby-on-rails-application/frontend-development/erb-basics
2 Answers
Drake Ramming
5,373 PointsSo given the correct answer code you ran the HTML output would look as follows:
Name:
Joe Name:
Stan Name:
Gus
The reason this is happening, is that you first have "Name:" which is followed by a line break which pushes the code to the next line.
To pass the quiz you only need to add the <%= status.name %> before the break tag.
<% @statuses.each do |status| %>
Name: <%= status.name %><br />
<% end %>
Which produces the following view:
Name: Joe
Name: Stan
Name: Gus
If you wanted to structure the data in a table as Jim did in the video and achieve the same view as directly above you could do the following:
<table>
<% @statuses.each do |status| %>
<tr>
<td>Name:</td>
<td><%= status.name %></td>
</tr>
<% end %>
</table>
jadaml
11,593 PointsThanks Drake. That makes more sense.
Ben Goldman
14,626 PointsBen Goldman
14,626 PointsThanks from me as well. That really helped a lot.