Updated Worker Show page to include Linked Participants
This commit is contained in:
parent
9066a968eb
commit
1265d885ec
|
@ -15,8 +15,6 @@ class ParticipantsController < ApplicationController
|
|||
@service_contract = ServiceContract.new # Initialize a new Service Contract object
|
||||
end
|
||||
|
||||
|
||||
|
||||
def new
|
||||
@participant = Participant.new
|
||||
end
|
||||
|
@ -59,6 +57,18 @@ class ParticipantsController < ApplicationController
|
|||
end
|
||||
end
|
||||
|
||||
def search
|
||||
if params[:term].present?
|
||||
@participants = Participant.where("first_name LIKE ? OR last_name LIKE ?", "%#{params[:term]}%", "%#{params[:term]}%")
|
||||
else
|
||||
@participants = Participant.none
|
||||
end
|
||||
|
||||
respond_to do |format|
|
||||
format.json { render json: @participants.map { |participant| { label: participant.full_name, value: participant.id } } }
|
||||
end
|
||||
end
|
||||
|
||||
def destroy
|
||||
@participant.destroy
|
||||
redirect_to participants_url, notice: 'Participant was successfully destroyed.'
|
||||
|
|
|
@ -4,9 +4,12 @@ class WorkersController < ApplicationController
|
|||
end
|
||||
|
||||
def show
|
||||
@worker = Worker.find(params[:id])
|
||||
@worker = Worker.includes(:participants).find(params[:id])
|
||||
@employments = @worker.employments
|
||||
@employment = Employment.new # Initialize a new Employment instance
|
||||
end
|
||||
|
||||
|
||||
|
||||
def new
|
||||
@worker = Worker.new
|
||||
end
|
||||
|
@ -50,6 +53,24 @@ class WorkersController < ApplicationController
|
|||
# Format the response as needed by your autocomplete component
|
||||
render json: @workers.map { |worker| { label: "#{worker.first_name} #{worker.last_name}", value: worker.id } }
|
||||
end
|
||||
|
||||
def link_participant
|
||||
@worker = Worker.find(params[:id])
|
||||
|
||||
# Assuming you're submitting the participant ID in your form
|
||||
participant_id = params[:employment][:participant_id]
|
||||
start_date = params[:employment][:start_date]
|
||||
end_date = params[:employment][:end_date]
|
||||
|
||||
# Create the employment relationship
|
||||
@employment = @worker.employments.build(participant_id: participant_id, start_date: start_date, end_date: end_date)
|
||||
|
||||
if @employment.save
|
||||
redirect_to @worker, notice: 'Participant was successfully linked to the worker.'
|
||||
else
|
||||
render 'show', alert: 'Unable to link participant.'
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
def worker_params
|
||||
|
|
|
@ -9,6 +9,9 @@ class Participant < ApplicationRecord
|
|||
# Validations
|
||||
validates :first_name, presence: true
|
||||
validates :last_name, presence: true
|
||||
def full_name
|
||||
"#{first_name} #{last_name}"
|
||||
end
|
||||
|
||||
# Other associations
|
||||
has_and_belongs_to_many :programs
|
||||
|
|
|
@ -1,8 +1,10 @@
|
|||
class Worker < ApplicationRecord
|
||||
# One-to-many relationship with Participants
|
||||
has_many :participants, through: :employments
|
||||
# One-to-many relationship with Employments
|
||||
has_many :employments
|
||||
|
||||
# Many-to-many relationship with Participants through Employments
|
||||
has_many :participants, through: :employments
|
||||
|
||||
# Many-to-many relationship with Employers through Participants
|
||||
has_many :employers, through: :participants
|
||||
|
||||
|
|
|
@ -43,3 +43,93 @@
|
|||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
|
||||
<div class="row mt-4">
|
||||
<div class="col-md-6">
|
||||
<h2 class="mt-4">Linked Participants</h2>
|
||||
<% if @employments.present? %>
|
||||
<table class="table table-striped">
|
||||
<thead class="table-light">
|
||||
<tr>
|
||||
<th>Participant Name</th>
|
||||
<th>Start Date</th>
|
||||
<th>End Date</th>
|
||||
<th>Actions</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<% @employments.each do |employment| %>
|
||||
<tr>
|
||||
<% participant = employment.participant %>
|
||||
<td><%= participant ? "#{participant.first_name} #{participant.last_name}" : 'No Participant Assigned' %></td>
|
||||
<td><%= employment.start_date.strftime('%B %d, %Y') if employment.start_date.present? %></td>
|
||||
<td><%= employment.end_date.strftime('%B %d, %Y') if employment.end_date.present? %></td>
|
||||
<td>
|
||||
<%= link_to 'View', participant_path(participant), class: 'btn btn-sm btn-secondary' %>
|
||||
<%= link_to 'Edit', edit_employment_path(employment), class: 'btn btn-sm btn-info' %>
|
||||
</td>
|
||||
</tr>
|
||||
<% end %>
|
||||
</tbody>
|
||||
</table>
|
||||
<% else %>
|
||||
<p>No employments available.</p>
|
||||
<% end %>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
|
||||
<%= form_with(model: [@worker, @employment], url: link_participant_worker_path(@worker), 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 'employment[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 %>
|
||||
|
||||
<%# 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>
|
||||
|
||||
|
||||
|
||||
|
|
|
@ -10,16 +10,25 @@ Rails.application.routes.draw do
|
|||
end
|
||||
|
||||
resources :participants do
|
||||
collection do
|
||||
get 'search' # Define search on the collection, not on a member
|
||||
end
|
||||
|
||||
member do
|
||||
post 'link_worker'
|
||||
post 'link_vendor'
|
||||
post 'link_worker' # Other member routes
|
||||
post 'link_vendor' #Add this line to link a vendor
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
resources :workers do
|
||||
member do
|
||||
post 'link_participant'
|
||||
end
|
||||
get 'search', on: :collection
|
||||
end
|
||||
|
||||
|
||||
resources :vendors do
|
||||
collection do
|
||||
get 'search'
|
||||
|
|
Loading…
Reference in New Issue