From d8fe866b682c8569ec44ed011e102beaedf0246a Mon Sep 17 00:00:00 2001 From: Ben Date: Tue, 30 Jan 2024 16:34:18 -0600 Subject: [PATCH] Employer module updated and now working as intended with updated fields, proper saving and design elements. --- app/controllers/employers_controller.rb | 19 +++++++++---- app/controllers/participants_controller.rb | 32 ++++++++++++++++------ app/views/employers/edit.html.erb | 11 ++++---- app/views/employers/index.html.erb | 11 ++++---- app/views/employers/show.html.erb | 5 ++-- 5 files changed, 50 insertions(+), 28 deletions(-) diff --git a/app/controllers/employers_controller.rb b/app/controllers/employers_controller.rb index 8d46786..88ba859 100644 --- a/app/controllers/employers_controller.rb +++ b/app/controllers/employers_controller.rb @@ -48,14 +48,17 @@ class EmployersController < ApplicationController # This action responds to the auto-complete AJAX requests def search if params[:term].present? - @employers = Employer.where("name LIKE ?", "%#{params[:term]}%") + @employers = Employer.where( + "first_name LIKE :term OR last_name LIKE :term", + term: "%#{params[:term]}%" + ) else @employers = Employer.none end - - # Respond with a JSON array of employer names and IDs - render json: @employers.map { |e| { label: e.name, value: e.id } } + + render json: @employers.map { |e| { label: e.full_name, value: e.id } } end + private @@ -64,6 +67,12 @@ class EmployersController < ApplicationController end def employer_params - params.require(:employer).permit(:name, :address, :phone, :email, :tin, :dob, :ssn, :gender) + params.require(:employer).permit( + :first_name, :last_name, + :address_line_1, :address_line_2, + :city, :state, :zip, + :phone, :email, :tin, :dob, :ssn, :gender + ) end + end diff --git a/app/controllers/participants_controller.rb b/app/controllers/participants_controller.rb index 8016eb7..7c10712 100644 --- a/app/controllers/participants_controller.rb +++ b/app/controllers/participants_controller.rb @@ -21,12 +21,18 @@ class ParticipantsController < ApplicationController def create @participant = Participant.new(participant_params) + Participant.transaction do if params[:is_employer] == 'yes' - employer = Employer.create!(employer_params_from_participant(@participant)) - @participant.employer = employer + employer = Employer.new(employer_params_from_participant(@participant)) + if employer.save + @participant.employer = employer + else + Rails.logger.debug "Employer creation failed: #{employer.errors.full_messages}" + raise ActiveRecord::Rollback, "Employer could not be saved" + end end - + if @participant.save redirect_to @participant, notice: 'Participant was successfully created.' else @@ -39,6 +45,7 @@ class ParticipantsController < ApplicationController render :new end + def edit end @@ -81,15 +88,24 @@ class ParticipantsController < ApplicationController def employer_params_from_participant(participant) { - name: "#{participant.first_name} #{participant.last_name}", - address: participant.address, + first_name: participant.first_name, + last_name: participant.last_name, + address_line_1: participant.address_line_1, + address_line_2: participant.address_line_2, + city: participant.city, + state: participant.state, + zip: participant.zip, phone: participant.phone, email: participant.email, - dob: participant.dob, # Mapping Date of Birth - ssn: participant.ssn, # Mapping SSN - gender: participant.gender # Mapping Gender + dob: participant.dob, + ssn: participant.ssn, + gender: participant.gender } end + + + + def employment_params params.require(:employment).permit(:worker_id, :start_date, :end_date) diff --git a/app/views/employers/edit.html.erb b/app/views/employers/edit.html.erb index 0aeab25..6b14af2 100644 --- a/app/views/employers/edit.html.erb +++ b/app/views/employers/edit.html.erb @@ -1,14 +1,13 @@
-
-
+
+

Edit Employer

<%= render 'form', employer: @employer %> -
- <%= link_to 'Show', @employer, class: 'btn btn-info' %> - <%= link_to 'Back to List', employers_path, class: 'btn btn-secondary' %> - <%= link_to 'Destroy', @employer, method: :delete, data: { confirm: 'Are you sure?', turbo: 'false' }, class: 'btn btn-danger' %> +
+ <%= link_to 'Back to List', employers_path, class: "btn btn-secondary" %> + <%= link_to 'Destroy', @employer, method: :delete, data: { confirm: 'Are you sure?' }, class: "btn btn-danger" %>
diff --git a/app/views/employers/index.html.erb b/app/views/employers/index.html.erb index 32e3eac..09db3d0 100644 --- a/app/views/employers/index.html.erb +++ b/app/views/employers/index.html.erb @@ -22,21 +22,20 @@ <% @employers.each do |employer| %> - <%= employer.first_name %> <%= employer.last_name %> + <%= employer.first_name %> + <%= employer.last_name %> <%= [employer.address_line_1, employer.address_line_2, employer.city, employer.state, employer.zip].compact.join(', ') %> <%= employer.phone %> <%= employer.email %> <%= employer.dob.strftime('%B %d, %Y') if employer.dob %> <%= link_to employer, class: 'btn btn-sm btn-secondary' do %> - + <% end %> - - <%= link_to edit_employer_path(employer), class: 'btn btn-sm btn-info' do %> - + <% end %> - + <% end %> diff --git a/app/views/employers/show.html.erb b/app/views/employers/show.html.erb index e48defb..5cab7a5 100644 --- a/app/views/employers/show.html.erb +++ b/app/views/employers/show.html.erb @@ -12,7 +12,7 @@

Address: - <%= [employer.address_line_1, employer.address_line_2, employer.city, employer.state, employer.zip].compact.join(', ') %> + <%= [@employer.address_line_1, @employer.address_line_2, @employer.city, @employer.state, @employer.zip].compact.join(', ') %>

@@ -48,9 +48,8 @@

- <%= link_to 'Edit', edit_employer_path(@employer), class: "btn btn-primary" %> + <%= link_to 'Edit', edit_employer_path(@employer), class: "btn btn-dark" %> <%= link_to 'Back to List', employers_path, class: "btn btn-secondary" %> - <%= link_to 'Destroy', @employer, method: :delete, data: { confirm: 'Are you sure?', turbo: 'false' }, class: "btn btn-danger" %>