diff options
| author | Raghuram Subramani <raghus2247@gmail.com> | 2025-10-30 18:24:37 -0400 |
|---|---|---|
| committer | Raghuram Subramani <raghus2247@gmail.com> | 2025-10-30 18:24:37 -0400 |
| commit | b92b736fb8799302f28a34cda820b99a5f68f766 (patch) | |
| tree | 63d263db264237feb171c2d4b3d6d2f058efcb81 | |
| parent | 461a210ab925f66b15f8998833acb3f514f4a17e (diff) | |
clients: edit page
| -rw-r--r-- | app/controllers/clients_controller.rb | 15 | ||||
| -rw-r--r-- | app/javascript/controllers/nested_fields_controller.js | 15 | ||||
| -rw-r--r-- | app/models/client.rb | 2 | ||||
| -rw-r--r-- | app/views/clients/_form.html.haml | 18 | ||||
| -rw-r--r-- | app/views/clients/_form_household_members.html.haml | 6 | ||||
| -rw-r--r-- | app/views/clients/edit.html.haml | 6 | ||||
| -rw-r--r-- | app/views/clients/show.html.haml | 1 | ||||
| -rw-r--r-- | config/routes.rb | 2 |
8 files changed, 58 insertions, 7 deletions
diff --git a/app/controllers/clients_controller.rb b/app/controllers/clients_controller.rb index 6110029..a8087c7 100644 --- a/app/controllers/clients_controller.rb +++ b/app/controllers/clients_controller.rb @@ -22,6 +22,19 @@ class ClientsController < ApplicationController end end + def edit + @client = Client.find(params[:id]) + end + + def update + @client = Client.find(params[:id]) + if @client.update(client_params) + redirect_to show_clients_path(uuid: @client.uuid), notice: 'Client Updated!' + else + render :edit, status: :unprocessable_entity + end + end + def find query = params[:q].downcase json = Client.where('lower(first_name) LIKE ? OR lower(last_name) LIKE ? OR mobile_number = ?', query, query, query) @@ -44,6 +57,8 @@ class ClientsController < ApplicationController :first_name, :last_name, :member_type, + :id, + :_destroy, ], ) end diff --git a/app/javascript/controllers/nested_fields_controller.js b/app/javascript/controllers/nested_fields_controller.js index 56f86f5..87f9bc3 100644 --- a/app/javascript/controllers/nested_fields_controller.js +++ b/app/javascript/controllers/nested_fields_controller.js @@ -8,6 +8,21 @@ export default class extends Controller { this.fieldsTarget.insertAdjacentHTML("beforeend", this.#templateContent); } + remove(e) { + e.preventDefault() + + const wrapper = e.target.closest('tr') + + if (wrapper.dataset.newRecord === "true") { + wrapper.remove() + } else { + wrapper.style.display = "none" + + const input = wrapper.querySelector("input[name*='_destroy']") + input.value = "1" + } + } + get #templateContent() { return this.templateTarget.innerHTML.replace(/__INDEX__/g, Date.now()); } diff --git a/app/models/client.rb b/app/models/client.rb index f16d382..3886ff5 100644 --- a/app/models/client.rb +++ b/app/models/client.rb @@ -2,5 +2,5 @@ class Client < ApplicationRecord has_many :visits has_many :household_members - accepts_nested_attributes_for :household_members + accepts_nested_attributes_for :household_members, allow_destroy: true, reject_if: :all_blank end diff --git a/app/views/clients/_form.html.haml b/app/views/clients/_form.html.haml index 42db615..ae8e65b 100644 --- a/app/views/clients/_form.html.haml +++ b/app/views/clients/_form.html.haml @@ -12,18 +12,26 @@ %th First Name %th Last Name %th Member Type + %th Actions %tbody{ 'data-nested-fields-target': 'fields' } + - @client.household_members.each do |member| + %tr + = simple_fields_for 'client[household_members_attributes][]', member, index: member.id do |hm_f| + = hm_f.hidden_field :id + = hm_f.hidden_field :_destroy + = render 'form_household_members', hm_f: hm_f + + %td + %button{ data: { action: 'nested-fields#remove' } } Remove %template{ 'data-nested-fields-target': 'template' } %tr = simple_fields_for 'client[household_members_attributes][]', HouseholdMember.new, index: '__INDEX__' do |hm_f| + = render 'form_household_members', hm_f: hm_f + %td - = hm_f.input :first_name, label: false - %td - = hm_f.input :last_name, label: false - %td - = hm_f.collection_radio_buttons :member_type, HouseholdMember::MEMBER_TYPES, :first, :last, label: false + %button{ data: { action: 'nested-fields#remove' } } Remove = button_tag "Add Member", type: :button, data: { action: "nested-fields#append" } diff --git a/app/views/clients/_form_household_members.html.haml b/app/views/clients/_form_household_members.html.haml new file mode 100644 index 0000000..6ef9523 --- /dev/null +++ b/app/views/clients/_form_household_members.html.haml @@ -0,0 +1,6 @@ +%td + = hm_f.input :first_name, label: false +%td + = hm_f.input :last_name, label: false +%td + = hm_f.collection_radio_buttons :member_type, HouseholdMember::MEMBER_TYPES, :first, :last, label: false diff --git a/app/views/clients/edit.html.haml b/app/views/clients/edit.html.haml new file mode 100644 index 0000000..e944f77 --- /dev/null +++ b/app/views/clients/edit.html.haml @@ -0,0 +1,6 @@ +%article + %header + %b Edit Client + + = simple_form_for @client, data: { controller: 'nested-fields' } do |f| + = render 'form', f: f diff --git a/app/views/clients/show.html.haml b/app/views/clients/show.html.haml index dd72ba0..05bf5b6 100644 --- a/app/views/clients/show.html.haml +++ b/app/views/clients/show.html.haml @@ -1,6 +1,7 @@ %article %header %b Client Information + = link_to "EDIT", edit_client_path(id: @client.id) %p %span diff --git a/config/routes.rb b/config/routes.rb index a4d68b9..ff15e67 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -14,7 +14,7 @@ Rails.application.routes.draw do # Defines the root path route ("/") root "home#index" - resources :clients, only: [ :new ] do + resources :clients, only: [ :new, :edit, :update ] do post 'create', on: :collection get 'show', on: :collection, as: 'show' get 'find', on: :collection |
