From e90c541ab48226b743616e68a099377ade3fa1af Mon Sep 17 00:00:00 2001 From: Mathieu Magnin Date: Tue, 30 Oct 2018 15:16:47 +0100 Subject: [PATCH 1/2] [Fix #2276] When service creation fails form is not cleared --- .../new_administrateur/services_controller.rb | 11 ++++++----- app/views/new_administrateur/services/new.html.haml | 2 +- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/app/controllers/new_administrateur/services_controller.rb b/app/controllers/new_administrateur/services_controller.rb index 24f61ce48..66d0c11da 100644 --- a/app/controllers/new_administrateur/services_controller.rb +++ b/app/controllers/new_administrateur/services_controller.rb @@ -7,18 +7,19 @@ module NewAdministrateur def new @procedure = procedure + @service = Service.new end def create - new_service = Service.new(service_params) - new_service.administrateur = current_administrateur + @service = Service.new(service_params) + @service.administrateur = current_administrateur - if new_service.save + if @service.save redirect_to services_path(procedure_id: params[:procedure_id]), - notice: "#{new_service.nom} créé" + notice: "#{@service.nom} créé" else @procedure = procedure - flash[:alert] = new_service.errors.full_messages + flash[:alert] = @service.errors.full_messages render :new end end diff --git a/app/views/new_administrateur/services/new.html.haml b/app/views/new_administrateur/services/new.html.haml index 635c38373..158eb8f73 100644 --- a/app/views/new_administrateur/services/new.html.haml +++ b/app/views/new_administrateur/services/new.html.haml @@ -8,4 +8,4 @@ %h1 Nouveau Service = render partial: 'form', - locals: { service: Service.new, procedure_id: @procedure.id } + locals: { service: @service, procedure_id: @procedure.id } From c656e36ec24c533ab4ff13a271c37f444a457a60 Mon Sep 17 00:00:00 2001 From: Mathieu Magnin Date: Tue, 30 Oct 2018 15:17:18 +0100 Subject: [PATCH 2/2] [Fix #2276] Test to ensure form is not cleared if service creation failed --- spec/controllers/new_administrateur/services_controller_spec.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/spec/controllers/new_administrateur/services_controller_spec.rb b/spec/controllers/new_administrateur/services_controller_spec.rb index 0e589b9a4..bddd7f679 100644 --- a/spec/controllers/new_administrateur/services_controller_spec.rb +++ b/spec/controllers/new_administrateur/services_controller_spec.rb @@ -43,6 +43,7 @@ describe NewAdministrateur::ServicesController, type: :controller do it { expect(flash.alert).not_to be_nil } it { expect(response).to render_template(:new) } + it { expect(assigns(:service).nom).to eq('super service') } end end