I'm creating a personally developed app, and I think it will be easier to use if you can see it in the order of popular posts. Basically, I implemented it while referring to this article.
routes.rb
routes.rb
get 'rank' => 'shops#rank'
In my case I set it to the rank action of the shops controller.
shops_controller.rb
shops_controller.rb
def rank
@all_ranks = Shop.find(Favorite.group(:id).order('count(shop_id) desc').limit(5).pluck(:shop_id))
end
Define rank method in shops controller. Display in the order of posts with the most likes.
rank.html.slim
ruby:rank.html.slim
- @all_ranks.each.with_index(1) do |shop, i|
h5.col-md-5
.card.shadow
| No.
= i
|
= link_to shop.name, shop_path(shop)
= link_to((image_tag shop.picture.url), shop_path(shop.id), class: 'shop-picture') if shop.picture.url.present?
Create a view file and display it. Excerpted as an example. After that, modify the appearance with CSS.
python
@all_ranks.each.with_index(1) do |shop, i|
From the above, you can display in descending order of likes.
The display in rank format has been completed easily.
Recommended Posts