LoginSignup
0
0

More than 3 years have passed since last update.

html.erbでナンバリング(番号をふる)をする方法

Posted at

HTMLの表示側で例えば、

  • 特徴1
  • 特徴1、特徴2
  • 特徴1、特徴2、特徴3…

のように内容によって、数値が変わるように番号をつけるときに使うコード。
(とくに利用が多いのはこういうときだった)

with_index(1) を利用する

こちらを利用することで、 ナンバリングスタート時の値を()内の値からスタートすることができ、
+1 と書く必要がなくなる。


<% ....map.with_index(1) do |a, i| %>


<p><%= i %></p>

each.with_index

こっちだと、 0 スタートになってしまうので、 +1 と書く必要が発生する。


<% ....map.each_with_index do |a, i| %>

<p><%= i + 1 %></p>
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0