今回は、大げさな機能ではないですが、 知っているとちょっと便利なHelper関数を紹介します。

cycle

まずはcycle。これはCSSのクラスにeven, oddを指定する場合のような、 周期的に繰り返す文字列を簡単に埋め込むことが出来ます。

   1  @items = [1,2,3,4]
   2    <table>
   3    <% @items.each do |item| %>
   4      <tr class="<%= cycle("even", "odd") -%>">
   5        <td>item</td>
   6      </tr>
   7    <% end %>
   8    </table>

pluralize

英語のみですが、数詞の語尾変化を適切に行ってくれます。

   1  pluralize(1, 'person')          # => 1 person
   2  pluralize(2, 'person')          # => 2 people
   3  pluralize(3, 'person', 'users') # => 3 users
   4  pluralize(0, 'person')          # => 0 people

posted by Png genki on Mon 3 Mar 2008 at 15:29

Comments:

or Preview
Social Bookmarks
  • Delicious
  • B_entry402
  • Clip_16_12_w
Services from s21g
twpro(ツイプロ)
Twitterプロフィールを快適検索
地価2009
土地の値段を調べてみよう
MyRestaurant
自分だけのレストラン手帳
Formula
ブログに数式を埋め込める数式コミュニティ