Even if I made a conditional branch in the view file, I noticed that if it was hit directly from the Url, it would jump to the page.
Use before_action on any controller to create a conditional branch before taking an action.
ruby:items.controller.rb
class ItemsController < ApplicationController
#before_Use action to make a conditional branch before jumping to the controller's action
before_action :correct_user, only: [:edit]
before_action :item_find, only: [:show, :edit, :update, :destroy]
def edit
end
private
def item_params
params.require(:item).permit(:title, :explain, :category_id, :condition_id, :price, :delivery_fee_id, :prefecture_id, :delivery_date_id, :image).merge(user_id: current_user.id)
end
# before_Users are selected by the method with action
def correct_user
@item = Item.find(params[:id])
if user_signed_in? && @item.user == current_user
render :edit
else
redirect_to root_path
end
end
If you hit the Url directly, you will always be asked to disclose the page to the controller via routing, so you need to describe it in the controller. I was able to reconfirm the flow of MVC once again.
Recommended Posts