From c7c70670f9cbedf6ef12acd06ed8388319590d1e Mon Sep 17 00:00:00 2001 From: David Gay Date: Sun, 13 Jun 2021 21:31:40 -0400 Subject: Only be able to view your own character --- app/controllers/characters_controller.rb | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) (limited to 'app/controllers') diff --git a/app/controllers/characters_controller.rb b/app/controllers/characters_controller.rb index 77e1a94..1a91988 100644 --- a/app/controllers/characters_controller.rb +++ b/app/controllers/characters_controller.rb @@ -1,8 +1,8 @@ class CharactersController < ApplicationController skip_before_action :redirect_if_no_active_character, only: [:new, :create] + before_action :set_character, only: [:show, :set_combat_styles] def show - @character = Character.find(params[:id]) end def new @@ -22,11 +22,6 @@ class CharactersController < ApplicationController end def set_combat_styles - @character = Character.find(params[:character_id]) - unless @character == current_char - flash[:alert] = "You can't set the combat styles of another character." - redirect_to character_path(@character) and return - end if @character.update(offensive_style: params[:offensive_style], defensive_style: params[:defensive_style]) flash[:notice] = "Changed combat styles to #{@character.offensive_style} and #{@character.defensive_style}." @@ -40,4 +35,12 @@ class CharactersController < ApplicationController def character_params params.require(:character).permit(:name) end + + def set_character + @character = Character.find(params[:character_id]) + unless current_char == @character + flash[:alert] = "You can only manage your own character." + redirect_to character_path(@character) + end + end end -- cgit v1.2.3