LoginSignup
3
3

More than 3 years have passed since last update.

[Rails]FullCalenderで投稿一覧も表示させつつ、マイページでは自分の投稿だけに絞ってカレンダーを表示する

Posted at

目的

FullCalenderを使って、Twitter+マイページでは自分の投稿をカレンダーで見る
ということを実装する

前提

フルカレンダーを実装し、カレンダーが表示できている
下記のサイトを参考に作りました。
https://qiita.com/sasasoni/items/fb0bc1644ece888ae1d4
https://qiita.com/imp555sti/items/ee9809768f6dc9439ab5

やること

FullCalenderをまずは準備しておきます。
そして自分が一番詰まったのは、indexではフォローしている人全員を表示する。
しかし自分のマイページのカレンダーでは、自分の投稿のみに限定する。
ということです。

どうやらFullCalenderの使い方としてindexアクションで定めた@eventsをカレンダーで表示するようなので
indexでは投稿一覧を@eventsではなく、別の名前を与えて。
ユーザーの投稿一覧を@eventsとしてあげます。

index.html.erb
def index
    @all_events = Event.all.includes(:user)
    @user = User.find(current_user.id)

   #フォローしているユーザーを取得
    @follow_users = @user.followings.map { |f| f[:id] }
    @follow_users << current_user.id

   #フォローユーザーの投稿のみ表示
    @events_onlyfollow = @all_events.where(user_id: @follow_users).order("created_at DESC")
    # 自分の投稿のみ
    @events = Event.where(user_id: current_user.id)
  end

まだまだ理解が浅くちゃんとできている訳ではないと思いますが。。。
とりあえずカレンダーが自分の投稿だけで絞れたのでひとまずOK

3
3
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
3
3