Loop in Ruby on Rails html.erb file

后端 未结 3 558
梦毁少年i
梦毁少年i 2021-02-05 01:43

everybody I\'m brand new with Ruby on Rails and I need to understand something. I have an instance variable (@users) and I need to loop over it inside an html.erb file a limitat

3条回答
  •  孤独总比滥情好
    2021-02-05 02:37

    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)
    

提交回复
热议问题