4
5

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

before_action :authenticate_user!, only: :searchの意味

Posted at
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)に移動してしまう。

4
5
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
4
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?