diff --git a/app/controllers/weights_controller.rb b/app/controllers/weights_controller.rb index cb5cd76..173186e 100644 --- a/app/controllers/weights_controller.rb +++ b/app/controllers/weights_controller.rb @@ -7,11 +7,14 @@ class WeightsController < ApplicationController # GET /weights/1.json def show if params[:wrestler] - Wrestler.update(params[:wrestler].keys, params[:wrestler].values) - redirect_to @weight.tournament + respond_to do |format| + Wrestler.update(params[:wrestler].keys, params[:wrestler].values) + format.html { redirect_to @weight, notice: 'Seeds were successfully updated.' } + end end @wrestlers = @weight.wrestlers @tournament = @weight.tournament + session[:return_path] = "/weights/#{@weight.id}" end # GET /weights/new diff --git a/app/controllers/wrestlers_controller.rb b/app/controllers/wrestlers_controller.rb index d5390fb..d47dd72 100644 --- a/app/controllers/wrestlers_controller.rb +++ b/app/controllers/wrestlers_controller.rb @@ -43,8 +43,12 @@ class WrestlersController < ApplicationController @weights = @school.tournament.weights respond_to do |format| if @wrestler.save - format.html { redirect_to @school, notice: 'Wrestler was successfully created.' } - format.json { render action: 'show', status: :created, location: @wrestler } + if session[:return_path] + format.html { redirect_to session.delete(:return_path), notice: 'Wrestler was successfully created.' } + else + format.html { redirect_to @school, notice: 'Wrestler was successfully created.' } + format.json { render action: 'show', status: :created, location: @wrestler } + end else format.html { render action: 'new' } format.json { render json: @wrestler.errors, status: :unprocessable_entity } @@ -61,8 +65,12 @@ class WrestlersController < ApplicationController @school = @wrestler.school respond_to do |format| if @wrestler.update(wrestler_params) - format.html { redirect_to @school, notice: 'Wrestler was successfully updated.' } - format.json { head :no_content } + if session[:return_path] + format.html { redirect_to session.delete(:return_path), notice: 'Wrestler was successfully updated.' } + else + format.html { redirect_to @school, notice: 'Wrestler was successfully updated.' } + format.json { render action: 'show', status: :created, location: @wrestler } + end else format.html { render action: 'edit' } format.json { render json: @wrestler.errors, status: :unprocessable_entity } @@ -95,8 +103,12 @@ class WrestlersController < ApplicationController @school = @wrestler.school @wrestler.destroy respond_to do |format| - format.html { redirect_to @school } - format.json { head :no_content } + if session[:return_path] + format.html { redirect_to session.delete(:return_path), notice: 'Wrestler was successfully deleted.' } + else + format.html { redirect_to @school, notice: 'Wrestler was successfully deleted.' } + format.json { head :no_content } + end end end diff --git a/test/controllers/weights_controller_test.rb b/test/controllers/weights_controller_test.rb index 8e1da47..7701439 100644 --- a/test/controllers/weights_controller_test.rb +++ b/test/controllers/weights_controller_test.rb @@ -7,6 +7,7 @@ class WeightsControllerTest < ActionController::TestCase @tournament = Tournament.find(1) # @tournament.generateMatchups @weight = @tournament.weights.first + @wrestler = @weight.wrestlers.first end def create @@ -53,6 +54,20 @@ class WeightsControllerTest < ActionController::TestCase assert_redirected_to '/static_pages/not_allowed' end + def delete_wrestler_from_weight_show_page + get :show, params: { id: @weight.id } + old_controller = @controller + @controller = WrestlersController.new + delete :destroy, params: { id: @wrestler.id } + @controller = old_controller + end + + test "redirect to weight show when deleting a wrestler from weight show" do + sign_in_owner + delete_wrestler_from_weight_show_page + assert_redirected_to "/weights/#{@weight.id}" + end + test "logged in tournament owner should get edit weight page" do sign_in_owner get_edit