-
Notifications
You must be signed in to change notification settings - Fork 0
Using .each in Rails
richlewis14 edited this page Nov 4, 2012
·
1 revision
Using .each allows you to render multiple records in the view, note, if you are only rendering 1 record there is no need to use the .each method
Examples
Rendering multiple records within the view
<% @latestrecipe.each do |r| %>
<li><%= r.dish_name %></li>
<li><%= r.country_of_origin %></li>
<li><%= r.difficulty %></li>
<li><%= r.preperation_time %></li>
<li><%= ingredient_names(r.ingredients) %></li>
<li><%= preperation_steps(r.preperations) %></li>
<% end %>
In the relevant controller
@latestrecipe = Recipe.order("created_at desc").limit(5)
Rendering one record within the view
<li><%= @featurerecipe.dish_name %></li>
In the relevant controller
@featurerecipe = Recipe.offset(rand(Recipe.count)).first