LoginSignup
0
0

More than 5 years have passed since last update.

params[:id]とfind_byメソッドの意味~ヘルスケアwebサービスを自分で作る医者の日記~

Last updated at Posted at 2019-06-27

progate 9回 ラスト

URLの指定で、他人の投稿にアクセスできてしまいのを防ぐ回

def ensure_correct_user
    @post = Post.find_by(id: params[:id])
    if @post.user_id != @current_user.id
      flash[:notice] = "権限がありません"
      redirect_to("/posts/index")
    end

find_byメソッドの意味がスッキリした
SQLの id というカラムから、params[:id]に合致するものを探し、
それをインスタンス変数として@post に渡している。
これは投稿そのもの、一列丸ごと 

ここでparams[:id]にする意味がわからなかったが

params[:id]はルートの指定を格納している
posts/1
みたいな

ユーザー各ページのURLを入れ込むことによって、ここにアクセスすることを防いでいる
と判明!!

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