1
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 3 years have passed since last update.

ユーザ一覧で"フォローする"ボタンを押したときの挙動 備忘録

Last updated at Posted at 2019-11-14
user/index/html.erb

      <% if user != current_user %>

        <% if current_user.following?(user) %>
            <%= link_to 'フォロー解除', unfollows_path(user.id), method: :POST %>
        <% else %>
            <%= link_to 'フォローする', follows_path(user.id), method: :POST %>
        <% end %>

      <% end %>

上の 'フォローする' ボタンを押すと、follows_path が読み込まれる

follows_pathは

routes.rb

post 'follows/:id' => 'relationships#follows', as: "follows" # フォローする。follows_pathの作成。

上の as "follows"で名前付きパスをつけたもの。
follows_path が呼び出されると、
relationships#follows(relationshipsコントローラのfollowsアクション)
が呼び出される

relationships.controller.rb
  # ユーザーをフォローする
  def follows(user_id)
    follower.create(followed_id: user_id)
  end

フォロー完了。

1
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
1
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?