仅使用Rails pluralize helper将强大的样式应用于数字

hyp*_*jas 2 ruby ruby-on-rails ruby-on-rails-3.1

我有这个:

<%= link_to biscuits_path do %>
<strong> <%= pluralize @biscuit.count, t('biscuits') %> </strong>
<%= end %>
Run Code Online (Sandbox Code Playgroud)

结果是一个像这样的链接:

<a href="/en/biscuits"> <strong> 2 Biscuits </strong></a>
Run Code Online (Sandbox Code Playgroud)

我想要一个仅在数字上强大的链接,例如:

<a href="/en/biscuits"> <strong> 2 </strong> Biscuits </a>
Run Code Online (Sandbox Code Playgroud)

我该怎么做?

Ped*_*nto 13

使用核心扩展中的String复数更容易,更清晰ActiveSupport:

<%= link_to biscuits_path do %>
  <strong><%= @biscuit.size %></strong> <%= "biscuit".pluralize(@biscuit.size)%>
<% end %>
Run Code Online (Sandbox Code Playgroud)

此外,你不需要<%= end %>.<% end %>很好.

  • 注意:`String#pluralize`在Rails 3.2之前没有开始使用可选的count参数. (2认同)