[RUBY] [Rails] How to put a crown mark on the ranking function

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.

Put a king mark


      <th class= "text-center">Ranking</th>
      <th class= "text-center">Note</th>
    <% @all_ranks.each.with_index(1) do |note, index| %>
        <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 class="text-center">
          <%= link_to user_note_path(note) do %>
            <%= note.name %>
          <% end %>
    <% end %>


<% 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
What to do if none of the values ​​match

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

[Rails] How to put a crown mark on the ranking function
(Ruby on Rails6) Create a function to edit the posted content
How to make a follow function in Rails
[Ruby on Rails] How to change the column name
A note about the seed function of Ruby on Rails
How to display a graph in Ruby on Rails (LazyHighChart)
How to run React and Rails on the same server
How to deploy a Rails application on AWS (article summary)
How to deploy jQuery on Rails
How to use Ruby on Rails
How to deploy Bootstrap on Rails
Put a badge on the icon
Rails on Tiles (how to write)
How to add the delete function
How to create a registration / update function where the table crosses
I want to add a browsing function with ruby on rails
[Rails / Heroku / MySQL] How to reset the DB of Rails application on Heroku
A memorandum on how to use Eclipse
How to redo a deployment on Heroku
[Java] How to use the hasNext function
How to put out the error bundling
How to insert a video in Rails
[Rails] How to use the map method
[Swift] How to implement the countdown function
How to change the timezone on Ubuntu
[Ruby on Rails] How to use redirect_to
[rails] How to create a partial template
How to implement ranking functionality in Rails
[Processing × Java] How to use the function
[Ruby on Rails] How to use kaminari
How to create a query using variables in GraphQL [Using Ruby on Rails]
How to solve the local environment construction of Ruby on Rails (MAC)!
[Rails] How to create a table, add a column, and change the column type
How to debug the processing in the Ruby on Rails model only on the console
How to build a Ruby on Rails development environment with Docker (Rails 5.x)
[Ruby On Rails] How to search the contents of params using include?
[Rails 6] Ranking function
[Rails] How to decide the destination by "rails routes"
Rails: How to write a rake task nicely
[Rails] How to write when making a subquery
How to deploy a container on AWS Lambda
[Rails] How to create a graph using lazy_high_charts
Add a tag function to Rails. Use acts-as-taggable-on
[swift5] How to implement the Twitter share function
How to implement the breadcrumb function using gretel
[For beginners] How to implement the delete function
How to implement a like feature in Rails
How to easily create a pull-down in Rails
[Swift] How to implement the fade-in / out function
[Rails] How to create a Twitter share button
[Rails MySQL] How to reset DB on heroku
[Ruby on Rails] How to install Bootstrap in Rails
How to build a Pytorch environment on Ubuntu
[Ruby on Rails] How to use session method
[Java] Memo on how to write the source
How to check Rails commands in the terminal
[Rails] How to easily introduce slick (slider function)
[Ruby on Rails] How to make the link destination part of the specified id
How to resolve errors that occur in the "Ruby on Rails" integration test
[Ruby on Rails] Rails tutorial Chapter 14 Summary of how to implement the status feed
How to implement image posting function using Active Storage in Ruby on Rails