class TopController < ReviewController
before_action :authenticate_user!, only: :search
def index
@products = Product.order('id ASC').limit(20)
end
def search
@products = Product.where('title LIKE(?)' , "%#{params[:keyword]}%").limit(20)
end
def entry
@product = Product.find(entry_params[:id])
end
def post
Review.create(create_params)
redirect_to action: :index
end
private
def search_params
params.permit(:keyword)
end
def entry_params
params.permit(:id)
end
def create_params
params.permit(:nickname, :product_id, :rate, :review)
end
end
before_actionや:authenticate_user!の使い方
あるボタンを押した時、ログインしていなければ他の画面に映るということ実行したい。
そこで使えるのが、:authenticate_use!である。
before_action :authenticate_user!, only: :search
ここで意味しているのは、サーチの時にだけ、アクションの前にログインしているかチェックしろということである。:authenticate_user!は自動でログイン画面(session_user)に移動してしまう。