From 9f2ee7f2d0e2b9dcc87d8560bddf759935860d69 Mon Sep 17 00:00:00 2001 From: natdeanlewissoftwire Date: Fri, 9 Sep 2022 13:52:24 +0100 Subject: [PATCH] feat: remove url query parsing, put validation before values added to db --- app/controllers/locations_controller.rb | 23 +++++++++-------------- 1 file changed, 9 insertions(+), 14 deletions(-) diff --git a/app/controllers/locations_controller.rb b/app/controllers/locations_controller.rb index ccd80d9cf..bf3bd5a63 100644 --- a/app/controllers/locations_controller.rb +++ b/app/controllers/locations_controller.rb @@ -57,7 +57,11 @@ class LocationsController < ApplicationController render_not_found and return unless @location && @scheme page = params[:location][:page] - if @location.update(location_params) + if page == "edit-local-authority" && !valid_location_admin_district?(location_params) + error_message = I18n.t("validations.location_admin_district") + @location.errors.add :location_admin_district, error_message + render :edit_local_authority, status: :unprocessable_entity + elsif @location.update(location_params) case page when "edit" if @location.location_admin_district.nil? @@ -70,21 +74,12 @@ class LocationsController < ApplicationController when "edit-name" redirect_to(scheme_check_answers_path(@scheme, anchor: "locations")) when "edit-local-authority" - if valid_location_admin_district?(location_params) - @uri_query = URI.parse(request.referer).query - @query_hash = @uri_query ? CGI.parse(@uri_query) : { "add_another_location": [] } - @add_another_location = @query_hash["add_another_location"].try(:first) - if @add_another_location == "Yes" - redirect_to(new_location_path(@location.scheme)) - else - redirect_to(scheme_check_answers_path(@scheme, anchor: "locations")) - end + if params[:add_another_location] == "Yes" + redirect_to(new_location_path(@location.scheme)) else - error_message = I18n.t("validations.location_admin_district") - @location.errors.add :location_admin_district, error_message - render :edit_local_authority, status: :unprocessable_entity + redirect_to(scheme_check_answers_path(@scheme, anchor: "locations")) + end end - end else render :edit, status: :unprocessable_entity end