如何将两种不同的样式应用于“投票”文本和此RoR代码的数字输出:
<%= pluralize video.vote_sum, 'Vote' %>
答案 0 :(得分:5)
由于您想要设置复数词的样式,我建议您执行以下操作:
<%= video.vote_sum %>
<div class="style-word">
<% word = "Vote" %>
<% if video.vote_sum > 1 %>
<%= word.pluralize %>
<% else %>
<%= word %>
<% end %>
</div>
(抱歉所有&lt; %%&gt;,我现在已经习惯了HAML,我不再做erb了。)
答案 1 :(得分:1)
试试这个:
<div class="<%= pluralize video.vote_sum, 'number' %>">
<%= video.vote_sum %>
</div>
<div class="<%= pluralize video.vote_sum, 'vote' %>">
<%= pluralize video.vote_sum, 'Vote' %>
</div>
在你的CSS中:
.vote{# some style code here!}
.votes{# some style code here!}
.number{# some style code here!}
.numbers{# some style code here!}
答案 2 :(得分:0)
<%= pluralize video.vote_sum, '<span class="some_other_class">Vote</span>', '<span class="some_other_class">Votes</span>' %>
答案 3 :(得分:0)
我刚遇到这种情况,并选择在pluralize
中扩展application_helper.rb
方法
def pluralize(count, singular, plural = nil)
count, counted = super.split(' ', 2)
[content_tag(:span, count, class: 'count'),content_tag(:span, counted, class: 'counted')].join(' ').html_safe
end
pluralize(2, "Thing")
现在会输出类似<span class="count">1</span> <span class="counted">Things</span>