hirolibe / hiro-blogapp

0 stars 0 forks source link

疑問(follow!(params[:account_id])) #48

Open hirolibe opened 1 month ago

hirolibe commented 1 month ago

引数として「params[:account_id]」を渡す場合、idの値が渡されるのか?

◯app/models/user.rb

def follow!(user)
  following_relationships.create!(following_id: user.id)
end

◯app/controllers/follows_controller.rb

class FollowsController < ApplicationController
  def create
    current_user.follow!(params[:account_id])
    redirect_to account_path(params[:account_id])
  end
end
hirolibe commented 1 month ago

引数に「Userクラスのインスタンス」が入る場合と、「user_id」が入る場合で場合分けする

class User < ApplicationRecord
  ・・・
  def follow!(user)
    if user.is_a?(User)
      user_id = user.id
    else
      user_id = user
    end

    following_relationships.create!(following_id: user_id)
  end
end