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 # This action responds to the auto-complete AJAX requests
def search def search
if params[:term].present? 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 else
@employers = Employer.none @employers = Employer.none
end end
# Respond with a JSON array of employer names and IDs render json: @employers.map { |e| { label: e.full_name, value: e.id } }
render json: @employers.map { |e| { label: e.name, value: e.id } }
end end
private private
@ -64,6 +67,12 @@ class EmployersController < ApplicationController
end end
def employer_params 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
end end

View File

@ -21,12 +21,18 @@ class ParticipantsController < ApplicationController
def create def create
@participant = Participant.new(participant_params) @participant = Participant.new(participant_params)
Participant.transaction do Participant.transaction do
if params[:is_employer] == 'yes' if params[:is_employer] == 'yes'
employer = Employer.create!(employer_params_from_participant(@participant)) employer = Employer.new(employer_params_from_participant(@participant))
@participant.employer = employer 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 end
if @participant.save if @participant.save
redirect_to @participant, notice: 'Participant was successfully created.' redirect_to @participant, notice: 'Participant was successfully created.'
else else
@ -39,6 +45,7 @@ class ParticipantsController < ApplicationController
render :new render :new
end end
def edit def edit
end end
@ -81,15 +88,24 @@ class ParticipantsController < ApplicationController
def employer_params_from_participant(participant) def employer_params_from_participant(participant)
{ {
name: "#{participant.first_name} #{participant.last_name}", first_name: participant.first_name,
address: participant.address, 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, phone: participant.phone,
email: participant.email, email: participant.email,
dob: participant.dob, # Mapping Date of Birth dob: participant.dob,
ssn: participant.ssn, # Mapping SSN ssn: participant.ssn,
gender: participant.gender # Mapping Gender gender: participant.gender
} }
end end
def employment_params def employment_params
params.require(:employment).permit(:worker_id, :start_date, :end_date) params.require(:employment).permit(:worker_id, :start_date, :end_date)

View File

@ -1,14 +1,13 @@
<div class="container mt-5"> <div class="container mt-5">
<div class="row"> <div class="row justify-content-center">
<div class="col"> <div class="col-md-6">
<h1 class="mb-4 text-center">Edit Employer</h1> <h1 class="mb-4 text-center">Edit Employer</h1>
<%= render 'form', employer: @employer %> <%= render 'form', employer: @employer %>
<div class="mt-3 d-flex justify-content-between align-items-center"> <div class="mt-3 d-flex justify-content-between">
<%= link_to 'Show', @employer, class: 'btn btn-info' %> <%= link_to 'Back to List', employers_path, class: "btn btn-secondary" %>
<%= 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" %>
<%= link_to 'Destroy', @employer, method: :delete, data: { confirm: 'Are you sure?', turbo: 'false' }, class: 'btn btn-danger' %>
</div> </div>
</div> </div>
</div> </div>

View File

@ -22,21 +22,20 @@
<tbody> <tbody>
<% @employers.each do |employer| %> <% @employers.each do |employer| %>
<tr> <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.address_line_1, employer.address_line_2, employer.city, employer.state, employer.zip].compact.join(', ') %></td>
<td><%= employer.phone %></td> <td><%= employer.phone %></td>
<td><%= employer.email %></td> <td><%= employer.email %></td>
<td><%= employer.dob.strftime('%B %d, %Y') if employer.dob %></td> <td><%= employer.dob.strftime('%B %d, %Y') if employer.dob %></td>
<td> <td>
<%= link_to employer, class: 'btn btn-sm btn-secondary' do %> <%= 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 %> <% end %>
<%= link_to edit_employer_path(employer), class: 'btn btn-sm btn-info' do %> <%= 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 %> <% end %>
</td> </td>
</tr> </tr>
<% end %> <% end %>
</tbody> </tbody>

View File

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