0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

いいねした記事一覧を表示

Posted at

「いいね」した記事一覧を表示

①config/routes.rb
  resources :favorites, only: [:index]

②app/models/user.rb

  has_many :favorites_articles, through: :likes, source: :article

これで、自分がいいねした記事だけを取得できる!!
③app/controllers/favorites_controller.rb
作成

Class FavoritesController < ApplicationController
  before_action :authenticate_user!

  def index
    @articles = current_user.favorites_articles
  end
end

④app/views/favorites/index.html.haml
作成

.container 
  %h2
    お気に入りの記事
  - @articles.each do |article|
    = render 'commons/article', article: article

⑤app/views/layouts/application.html.haml

  = link_to 'お気に入り', favorites_path

これで、お気に入りの一覧を表示できるようになりました👏

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?