Linked Workers to Employers and functionality is working as intended.

This commit is contained in:
Ben 2024-02-05 18:49:36 -06:00
parent ea9d032ad4
commit d0081c9d11
4 changed files with 144 additions and 8 deletions

View File

@ -30,6 +30,15 @@ class EmployerRecordsController < ApplicationController
end
end
def link_participant
@employer_record = EmployerRecord.new(employer_record_params)
if @employer_record.save
redirect_to employer_path(@employer_record.employer), notice: 'Participant was successfully linked.'
else
render 'show' # Or where you want to redirect in case of failure
end
end
def destroy
if @employer_record
@ -49,6 +58,6 @@ class EmployerRecordsController < ApplicationController
end
def employer_record_params
params.require(:employer_record).permit(:participant_id, :employer_id, :start_date, :end_date)
params.require(:employer_record).permit(:employer_id, :participant_id, :start_date, :end_date)
end
end

View File

@ -8,7 +8,9 @@ class EmployersController < ApplicationController
# GET /employers/:id
def show
Rails.logger.debug "Employer ID: #{params[:id]}"
@employer = Employer.includes(:employer_records, :direct_participants).find(params[:id])
Rails.logger.debug "Employer: #{@employer.inspect}"
@employer_records = @employer.employer_records.includes(:participant)
end
@ -68,8 +70,31 @@ class EmployersController < ApplicationController
# Handle the logic for creating the employer_record
# ...
end
def link_worker
@employer = Employer.find(params[:id])
participant_id = params[:employer_record][:participant_id] # Assuming this is passed from the form
start_date = params[:employer_record][:start_date]
end_date = params[:employer_record][:end_date]
employer_record = EmployerRecord.new(
employer: @employer,
participant_id: participant_id,
start_date: start_date,
end_date: end_date
)
if employer_record.save
redirect_to @employer, notice: 'Worker was successfully linked.'
else
flash.now[:alert] = employer_record.errors.full_messages.to_sentence
render :show
end
rescue ActiveRecord::RecordNotFound
# Handle case where Employer is not found
redirect_to some_path, alert: 'Employer not found.'
end
private
def set_employer

View File

@ -96,7 +96,31 @@
<% else %>
<p>No linked participants.</p>
<% end %>
</div>
<%= form_with(model: [@employer, EmployerRecord.new], url: link_participant_employer_records_path, method: :post, class: 'row g-3') do |form| %>
<div class="col-md-6">
<%= form.label :participant_name, "Add New Participant", class: 'form-label' %>
<%= text_field_tag :participant_name, nil, id: 'participant-autocomplete', class: 'form-control', placeholder: 'Start typing participant name...' %>
<%= hidden_field_tag 'employer_record[participant_id]', nil, id: 'selected-participant-id' %>
</div>
<div class="col-md-3">
<%= form.label :start_date, class: 'form-label' %>
<%= form.date_field :start_date, class: 'form-control' %>
</div>
<div class="col-md-3">
<%= form.label :end_date, class: 'form-label' %>
<%= form.date_field :end_date, class: 'form-control' %>
</div>
<div class="col-12">
<%= form.submit "Link Participant", class: 'btn btn-dark' %>
</div>
<% end %>
</div>
<div class="col-md-6">
<h2 class="mt-4">Linked Workers</h2>
@ -133,11 +157,86 @@
<% else %>
<p>No linked workers.</p>
<% end %>
<%= form_with(model: EmployerRecord.new, url: link_worker_employer_path(@employer), method: :post, class: 'row g-3') do |form| %>
<div class="col-md-6">
<%= form.label :worker_name, "Add New Worker", class: 'form-label' %>
<%= text_field_tag :worker_name, nil, id: 'worker-autocomplete', class: 'form-control', placeholder: 'Start typing worker name...' %>
<%= hidden_field_tag 'employer_record[worker_id]', nil, id: 'selected-worker-id' %>
</div>
<div class="col-md-3">
<%= form.label :start_date, class: 'form-label' %>
<%= form.date_field :start_date, class: 'form-control', name: 'employer_record[start_date]' %>
</div>
<div class="col-md-3">
<%= form.label :end_date, class: 'form-label' %>
<%= form.date_field :end_date, class: 'form-control', name: 'employer_record[end_date]' %>
</div>
<div class="col-12">
<%= form.submit "Link Worker", class: 'btn btn-dark' %>
</div>
<% end %>
</div>
</div>
</div>
<%# JavaScript for Participant Autocomplete %>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script src="https://code.jquery.com/ui/1.13.0/jquery-ui.min.js"></script>
<link rel="stylesheet" href="//code.jquery.com/ui/1.13.0/themes/base/jquery-ui.css">
<script>
$(document).ready(function() {
$('#participant-autocomplete').autocomplete({
source: function(request, response) {
$.ajax({
url: '/participants/search', // Updated endpoint for searching participants
dataType: "json",
data: { term: request.term },
success: function(data) {
response(data);
}
});
},
minLength: 2, // Minimum characters to trigger the search
select: function(event, ui) {
// Function to handle selection
$('#participant-autocomplete').val(ui.item.label); // ui.item.label should contain the name of the participant
$('#selected-participant-id').val(ui.item.value); // Set the participant ID in the hidden field
return false;
}
});
});
</script>
<%# JavaScript for Worker Autocomplete %>
<script>
$(document).ready(function() {
$('#worker-autocomplete').autocomplete({
source: function(request, response) {
$.ajax({
url: '/workers/search', // Endpoint for searching workers
dataType: "json",
data: { term: request.term },
success: function(data) {
response(data);
}
});
},
minLength: 2, // Minimum characters to trigger the search
select: function(event, ui) {
// Function to handle selection
$('#worker-autocomplete').val(ui.item.label); // Worker's name
$('#selected-worker-id').val(ui.item.value); // Worker's ID
return false;
}
});
});
</script>

View File

@ -37,14 +37,17 @@ Rails.application.routes.draw do
end
resources :employments, only: [:edit, :update, :destroy]
resources :employer_records, only: [:edit, :update, :destroy] do
collection do
post 'link_participant'
end
post 'link_participant', on: :collection
end
resources :service_contracts
resources :employers do
member do
post "link_worker"
end
get 'search', on: :collection
end