From a0b0fd471444fb83795799965b16ea5dc7a0228c Mon Sep 17 00:00:00 2001 From: Louis Knapp Date: Thu, 13 Jul 2017 19:07:01 -0500 Subject: [PATCH] lk | redirect to new form when successfully creating a client - allow for easier bulk creation --- app/controllers/clients_controller.rb | 2 +- .../clients/{_form.html.haml => _edit_form.html.haml} | 2 +- app/views/clients/_new_form.html.haml | 11 +++++++++++ app/views/clients/edit.html.haml | 2 +- app/views/clients/new.html.haml | 2 +- app/views/static_pages/home.html.haml | 2 ++ spec/controllers/clients_controller_spec.rb | 8 ++++++++ 7 files changed, 25 insertions(+), 4 deletions(-) rename app/views/clients/{_form.html.haml => _edit_form.html.haml} (85%) create mode 100644 app/views/clients/_new_form.html.haml diff --git a/app/controllers/clients_controller.rb b/app/controllers/clients_controller.rb index 8bd2448..1c8612d 100644 --- a/app/controllers/clients_controller.rb +++ b/app/controllers/clients_controller.rb @@ -16,7 +16,7 @@ class ClientsController < ApplicationController def create @client = Client.new(client_params) if @client.save - redirect_to edit_client_url(@client), notice: 'Client was successfully created.' + redirect_to new_client_path, notice: 'Client was successfully created.' else render action: 'new' end diff --git a/app/views/clients/_form.html.haml b/app/views/clients/_edit_form.html.haml similarity index 85% rename from app/views/clients/_form.html.haml rename to app/views/clients/_edit_form.html.haml index 44d6596..e3928a0 100644 --- a/app/views/clients/_form.html.haml +++ b/app/views/clients/_edit_form.html.haml @@ -8,4 +8,4 @@ = render 'fields', f: f .row .actions.col-sm-offset-2 - = f.submit class: "btn btn-default" + = f.submit "Save Changes", class: "btn btn-default" diff --git a/app/views/clients/_new_form.html.haml b/app/views/clients/_new_form.html.haml new file mode 100644 index 0000000..0430558 --- /dev/null +++ b/app/views/clients/_new_form.html.haml @@ -0,0 +1,11 @@ += form_for @client, html: {class: 'form-horizontal'} do |f| + - if @client.errors.any? + #error_explanation + %h2= pluralize(@client.errors.count, "error") + " prohibited this client from being saved:" + %ul + - @client.errors.full_messages.each do |msg| + %li= msg + = render 'fields', f: f + .row + .actions.col-sm-offset-2 + = f.submit "Save And Add Another", class: "btn btn-default" diff --git a/app/views/clients/edit.html.haml b/app/views/clients/edit.html.haml index bf49440..8a0e963 100644 --- a/app/views/clients/edit.html.haml +++ b/app/views/clients/edit.html.haml @@ -1,5 +1,5 @@ .container %h1 Edit client = render 'receipt_button' if @client.bike_id - = render 'form' + = render 'edit_form' = link_to 'Back', clients_path diff --git a/app/views/clients/new.html.haml b/app/views/clients/new.html.haml index 12241ff..54bcb53 100644 --- a/app/views/clients/new.html.haml +++ b/app/views/clients/new.html.haml @@ -1,4 +1,4 @@ .container %h1 New Client - = render 'form' + = render 'new_form' = link_to 'Back', clients_path diff --git a/app/views/static_pages/home.html.haml b/app/views/static_pages/home.html.haml index ca4648e..34b6a6e 100644 --- a/app/views/static_pages/home.html.haml +++ b/app/views/static_pages/home.html.haml @@ -11,6 +11,8 @@ %br %h2 Freecyclery %br + = link_to "Add Freecyclery Clients", new_client_path + %br = link_to "Freecyclery Clients", clients_path %br = link_to "Freecyclery Agencies", agencies_path diff --git a/spec/controllers/clients_controller_spec.rb b/spec/controllers/clients_controller_spec.rb index 79e9659..a71e53f 100644 --- a/spec/controllers/clients_controller_spec.rb +++ b/spec/controllers/clients_controller_spec.rb @@ -8,6 +8,14 @@ describe ClientsController do sign_in user end + describe "POST #create" do + it "redirects to new_client_path on success" do + client_attributes = attributes_for :client + request = post :create, client: client_attributes + expect(request).to redirect_to action: :new + end + end + describe "#print_select" do it "only assigns clients with a bike and an agency" do client_with_bike_and_agency = create :client, bike: create(:bike), agency: create(:agency)