Loop in Ruby on Rails html.erb file
You could do
<% for i in 0..9 do %>
<%= @users[i].name %>
<% end %>
But if you need only 10 users in the view, then you can limit it in the controller itself
@users = User.limit(10)
If @users
has more elements than you want to loop over, you can use first
or slice
:
Using first
<% @users.first(10).each do |users| %>
<%= do something %>
<% end %>
Using slice
<% @users.slice(0, 10).each do |users| %>
<%= do something %>
<% end %>
However, if you don't actually need the rest of the users in the @users array, you should only load as many as you need by using limit
:
@users = User.limit(10)