ruby-on-rails - 枚举时的不同样式(使用 each_with_index)

标签 ruby-on-rails ruby

我用 each_with_index 在我的主页上显示了一个用户排名前 5 的排名。 我只设法将一种全局样式应用于 5 个等级。

<% @users_by_roi.take(5).each_with_index do |user, index| %>
  <div><class="ranking"> <%= index + 1 %></div>
  <div class="text-center">
    <%= link_to (image_tag user.picture.url(:medium), class: "small-avatar"), user_path(user) %>
  </div>
  <div><%= link_to "#{user.username}", user_path(user), class: "hot-tipster-link" %></div>
  <div class="green-roi"> + <%= user.roi.round(2) %> %</div>
<% end %>

如何将特定样式应用于等级 1、2 和 3(金、银、铜)?

谢谢

最佳答案

实际上,以我的拙见,最好的方法是在您的 CSS 中,因为它是样式信息,与您通常显示的数据无关。

为此,我鼓励您查看 n-th master网站,这是一个很好的引用。这是一个像您这样的标记的演示:

.wrapper {
  width: 100%;
}

.wrapper > div {
  background-color: blue;
  color: white;
  height: 25px;
  width: 500px;
}

.wrapper > div:first-child {
  background-color: gold;
}

.wrapper > div:nth-child(2) {
  background-color: silver;
}

.wrapper > div:nth-child(3) {
  background-color: red;
}
<div class="wrapper">
  <div>1. </div>
  <div>2. </div>
  <div>3. </div>
  <div>4. </div>
  <div>5. </div>
</div>

关于ruby-on-rails - 枚举时的不同样式(使用 each_with_index),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31929026/

相关文章:

ruby-on-rails - 在 rails 开发环境 smtp cpanel 上发送电子邮件 ruby

mysql - 数据库级别的 Rails 舍入 float

ruby-on-rails - Ruby 中的 Sql 值数组

ruby-on-rails - ruby rails 4.0.4 : How do I set up the Environment Variables?

ruby-on-rails - 有条件地将日期分隔符插入聊天消息数组

ruby-on-rails - "Processing by Contoller#method as */*"和 "Processing by BillsController#show as HTML"有什么区别

ruby-on-rails - 验证 Rails ActiveRecord 中的 bool 字段

ruby-on-rails - Rails 表单对象与 reform-rails 与集合不工作或验证

mysql - 将解析文本文件的数据插入mysql数据库

ruby-on-rails - 在 ruby​​ 中,如何创建一个具有选项散列的方法,我可以将符号传递给它?