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
Adnan Khan
326 PointsObject Variables
I am trying to understand the following code:
<div class="page-header"> <h1>All Statuses</h1> </div>
<% @statuses.each do |status| %> <div class="status"> <h2><%= status.name %></h2> <p> </p> </div> <% end %>
I can't figure out where we use the object variable @statuses, and where we just use 'status', as in status.name.
1 Answer
Nima Shariatian
1,760 Points@statuses is the instance variable we set in the controller. it contains a collection of @statuses.
in our view we loop over the @statuses using a plain simple ruby loop and assign each object to a variable called "status" we can name this anything such as
<% @statuses.each do |foo| %> <%= foo.name %> <% end %>
but we chose status for clarity. more info on ruby variables: http://www.railstips.org/blog/archives/2006/11/18/class-and-instance-variables-in-ruby/
here is a link on ruby loops: http://www.skorks.com/2009/09/a-wealth-of-ruby-loops-and-iterators/ (note that the "each iterator" is also there)
hope this helps.
Adnan Khan
326 PointsThanks for this.
Wilfried Hounyo
6,509 PointsWilfried Hounyo
6,509 Pointsif I am correct @statuses should contain all Statuses so you will use status only to parse the different parts of each status. Refer to this it might help: http://blog.teamtreehouse.com/how-to-use-the-each-method-in-ruby-on-rails-treehouse-quick-tip