This time I would like to write how to put a crown mark on the ranking.
-Implementation of ranking function. I referred to the following article. Please note that the following articles are used for the view methods described below. https://qiita.com/mitsumitsu1128/items/18fa5e49a27e727f00b4 -Put Font-Awesome 6 ready for use.
rank.html.erb
<table>
<thead>
<tr>
<th class= "text-center">Ranking</th>
<th class= "text-center">Note</th>
</tr>
</thead>
<tbody>
<% @all_ranks.each.with_index(1) do |note, index| %>
<tr>
<td class="text-center">
<% case index when 1 %>
<i class="fas fa-crown" style='color: gold;'></i>
<% when 2 %>
<i class="fas fa-crown" style='color: silver;'></i>
<% when 3 %>
<i class="fas fa-crown" style='color: orange;'></i>
<% else %>
<%= index %>
<% end %>
</td>
<td class="text-center">
<%= link_to user_note_path(note) do %>
<%= note.name %>
<% end %>
</td>
</tr>
<% end %>
</tbody>
</table>
<% case index when 1 %>
This is a ruby case statement. The "if" statement allows you to combine multiple conditional expressions to perform complex branching, but if you want to find a match among multiple candidates for one value, use the "case" statement. It is convenient to use. An example is as follows.
case Target object
when value 1
What to do if it matches the value 1
when value 2
What to do if it matches the value 2
when value 3
What to do if it matches the value 3
else
What to do if none of the values match
end
By using .with_index (1), the numbers are output in order from 1, so on the other hand, in the case statement like 1 for gold crown, 2 for silver crown, 3 for copper crown. We are implementing it.
Recommended Posts