Employer module updated and now working as intended with updated fields, proper saving and design elements.

This commit is contained in:
Ben 2024-01-30 16:34:18 -06:00
parent 66aaeb4ed6
commit d8fe866b68
5 changed files with 50 additions and 28 deletions

View File

@ -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

View File

@ -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)

View File

@ -1,14 +1,13 @@
<div class="container mt-5">
<div class="row">
<div class="col">
<div class="row justify-content-center">
<div class="col-md-6">
<h1 class="mb-4 text-center">Edit Employer</h1>
<%= render 'form', employer: @employer %>
<div class="mt-3 d-flex justify-content-between align-items-center">
<%= 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' %>
<div class="mt-3 d-flex justify-content-between">
<%= 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" %>
</div>
</div>
</div>

View File

@ -22,21 +22,20 @@
<tbody>
<% @employers.each do |employer| %>
<tr>
<td><%= employer.first_name %> <%= employer.last_name %></td>
<td><%= employer.first_name %></td>
<td><%= employer.last_name %></td>
<td><%= [employer.address_line_1, employer.address_line_2, employer.city, employer.state, employer.zip].compact.join(', ') %></td>
<td><%= employer.phone %></td>
<td><%= employer.email %></td>
<td><%= employer.dob.strftime('%B %d, %Y') if employer.dob %></td>
<td>
<%= link_to employer, class: 'btn btn-sm btn-secondary' do %>
<i class="bi bi-eye"></i> <!-- Eyeball icon for 'Show' -->
<i class="bi bi-eye"></i><!-- Eyeball icon for 'Show' -->
<% end %>
<%= link_to edit_employer_path(employer), class: 'btn btn-sm btn-info' do %>
<i class="bi bi-pencil-fill" style="color: white;"></i> <!-- Pencil icon for 'Edit' -->
<i class="bi bi-pencil-fill" style="color: white;"></i> <!-- Pencil icon for 'Edit' with white color -->
<% end %>
</td>
</td>
</tr>
<% end %>
</tbody>

View File

@ -12,7 +12,7 @@
<p class="card-text">
<strong>Address:</strong>
<%= [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(', ') %>
</p>
<p class="card-text">
@ -48,9 +48,8 @@
</div>
<div class="mt-3 d-flex justify-content-between">
<%= 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" %>
</div>
</div>
</div>