Fixed linked workers to be selectable and also now have them displaying in alpha order by last name
This commit is contained in:
parent
f474c91b8c
commit
8d59f7b788
|
@ -11,6 +11,8 @@ class ParticipantsController < ApplicationController
|
||||||
@participant = Participant.includes(:employments).find(params[:id])
|
@participant = Participant.includes(:employments).find(params[:id])
|
||||||
@workers = @participant.workers # Fetch associated workers
|
@workers = @participant.workers # Fetch associated workers
|
||||||
@employment = Employment.new # Initialize a new Employment object
|
@employment = Employment.new # Initialize a new Employment object
|
||||||
|
@participant = Participant.includes(employments: :worker).find(params[:id])
|
||||||
|
@employments = @participant.employments.joins(:worker).order('workers.last_name')
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -41,7 +41,8 @@ class WorkersController < ApplicationController
|
||||||
|
|
||||||
private
|
private
|
||||||
def worker_params
|
def worker_params
|
||||||
params.require(:worker).permit(:name, :address, :phone, :email, :dob, :ssn, :gender)
|
params.require(:worker).permit(:first_name, :last_name, :address, :phone, :email, :dob, :ssn, :gender)
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
|
@ -5,4 +5,13 @@ class Worker < ApplicationRecord
|
||||||
|
|
||||||
# Many-to-many relationship with Employers through Participants
|
# Many-to-many relationship with Employers through Participants
|
||||||
has_many :employers, through: :participants
|
has_many :employers, through: :participants
|
||||||
|
|
||||||
|
# Validations
|
||||||
|
validates :first_name, presence: true
|
||||||
|
validates :last_name, presence: true
|
||||||
|
|
||||||
|
# Method to return the full name of the worker
|
||||||
|
def full_name
|
||||||
|
"#{first_name} #{last_name}"
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -14,4 +14,6 @@
|
||||||
<%= form.submit "Update Employment" %>
|
<%= form.submit "Update Employment" %>
|
||||||
<% end %>
|
<% end %>
|
||||||
|
|
||||||
<%= link_to 'Back to Participant', participant_path(@employment.participant) %>
|
<%= link_to 'Delete Employment', employment_path(@employment), method: :delete, data: { confirm: 'Are you sure?' }, class: 'btn btn-danger' %>
|
||||||
|
|
||||||
|
<%= link_to 'Back to Participant', participant_path(@employment.participant), class: 'btn btn-secondary' %>
|
||||||
|
|
|
@ -10,7 +10,6 @@
|
||||||
<%= javascript_pack_tag 'application', 'data-turbolinks-track': 'reload' %>
|
<%= javascript_pack_tag 'application', 'data-turbolinks-track': 'reload' %>
|
||||||
<%= javascript_include_tag 'rails-ujs', 'data-turbolinks-track': 'reload' %>
|
<%= javascript_include_tag 'rails-ujs', 'data-turbolinks-track': 'reload' %>
|
||||||
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.3/font/bootstrap-icons.min.css">
|
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.3/font/bootstrap-icons.min.css">
|
||||||
|
|
||||||
</head>
|
</head>
|
||||||
|
|
||||||
<body class="bg-light-grey">
|
<body class="bg-light-grey">
|
||||||
|
@ -44,9 +43,7 @@
|
||||||
</div>
|
</div>
|
||||||
<% end %>
|
<% end %>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.bundle.min.js" integrity="sha384-C6RzsynM9kWDrMNeT87bh95OGNyZPhcTNXj1NW7RuBCsyN/o0jlpcV8Qyq46cDfL" crossorigin="anonymous"></script>
|
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.bundle.min.js" integrity="sha384-C6RzsynM9kWDrMNeT87bh95OGNyZPhcTNXj1NW7RuBCsyN/o0jlpcV8Qyq46cDfL" crossorigin="anonymous"></script>
|
||||||
|
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
|
|
@ -72,18 +72,18 @@
|
||||||
</tr>
|
</tr>
|
||||||
</thead>
|
</thead>
|
||||||
<tbody>
|
<tbody>
|
||||||
<% @participant.employments.each do |employment| %>
|
<% @employments.each do |employment| %>
|
||||||
<tr>
|
<tr>
|
||||||
<td><%= employment.worker&.name || 'No Worker Assigned' %></td>
|
<td><%= employment.worker&.full_name || 'No Worker Assigned' %></td>
|
||||||
<td><%= employment.start_date.strftime('%B %d, %Y') if employment.start_date.present? %></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><%= employment.end_date.strftime('%B %d, %Y') if employment.end_date.present? %></td>
|
||||||
<td>
|
<td>
|
||||||
<%= link_to edit_employment_path(employment), class: 'btn btn-sm btn-warning' do %>
|
<%= link_to worker_path(employment.worker), class: 'btn btn-sm btn-secondary' do %>
|
||||||
<i class="bi bi-pencil-fill"></i> <!-- Pencil icon -->
|
<i class="bi bi-eye"></i> <!-- Eyeball icon for 'Show' -->
|
||||||
<% end %>
|
<% end %>
|
||||||
<%= link_to employment, method: :delete, data: { confirm: 'Are you sure?' }, class: 'btn btn-sm btn-danger' do %>
|
<%= link_to edit_employment_path(employment), class: 'btn btn-sm btn-warning' do %>
|
||||||
<i class="bi bi-trash" style="color: black;"></i> <!-- Trash can icon with black color -->
|
<i class="bi bi-pencil-fill"></i> <!-- Pencil icon for 'Edit' -->
|
||||||
<% end %>
|
<% end %>
|
||||||
</td>
|
</td>
|
||||||
</tr>
|
</tr>
|
||||||
<% end %>
|
<% end %>
|
||||||
|
@ -91,10 +91,11 @@
|
||||||
</table>
|
</table>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
<%= form_with(model: [@participant, @employment], url: link_worker_participant_path(@participant), method: :post, class: 'row g-3') do |form| %>
|
<%= form_with(model: [@participant, @employment], url: link_worker_participant_path(@participant), method: :post, class: 'row g-3') do |form| %>
|
||||||
<div class="col-md-6">
|
<div class="col-md-6">
|
||||||
<%= form.label :worker_id, "Select Worker", class: 'form-label' %>
|
<%= form.label :worker_id, "Select Worker", class: 'form-label' %>
|
||||||
<%= form.collection_select :worker_id, Worker.all, :id, :name, { include_blank: true }, { class: 'form-select' } %>
|
<%= form.collection_select :worker_id, Worker.all, :id, :full_name, { include_blank: true, prompt: "Select a Worker" }, { class: 'form-select' } %>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="col-md-3">
|
<div class="col-md-3">
|
||||||
|
@ -111,4 +112,3 @@
|
||||||
<%= form.submit "Link Worker", class: 'btn btn-dark' %>
|
<%= form.submit "Link Worker", class: 'btn btn-dark' %>
|
||||||
</div>
|
</div>
|
||||||
<% end %>
|
<% end %>
|
||||||
|
|
||||||
|
|
|
@ -11,8 +11,13 @@
|
||||||
<% end %>
|
<% end %>
|
||||||
|
|
||||||
<div class="mb-3">
|
<div class="mb-3">
|
||||||
<%= form.label :name, class: 'form-label' %>
|
<%= form.label :first_name, class: 'form-label' %>
|
||||||
<%= form.text_field :name, class: 'form-control' %>
|
<%= form.text_field :first_name, class: 'form-control' %>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="mb-3">
|
||||||
|
<%= form.label :last_name, class: 'form-label' %>
|
||||||
|
<%= form.text_field :last_name, class: 'form-control' %>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="mb-3">
|
<div class="mb-3">
|
||||||
|
|
|
@ -8,7 +8,8 @@
|
||||||
<table class="table table-striped table-hover">
|
<table class="table table-striped table-hover">
|
||||||
<thead class="table-dark">
|
<thead class="table-dark">
|
||||||
<tr>
|
<tr>
|
||||||
<th>Name</th>
|
<th>First Name</th>
|
||||||
|
<th>Last Name</th>
|
||||||
<th>Address</th>
|
<th>Address</th>
|
||||||
<th>Phone</th>
|
<th>Phone</th>
|
||||||
<th>Email</th>
|
<th>Email</th>
|
||||||
|
@ -19,7 +20,8 @@
|
||||||
<tbody>
|
<tbody>
|
||||||
<% @workers.each do |worker| %>
|
<% @workers.each do |worker| %>
|
||||||
<tr>
|
<tr>
|
||||||
<td><%= worker.name %></td>
|
<td><%= worker.first_name %></td>
|
||||||
|
<td><%= worker.last_name %></td>
|
||||||
<td><%= worker.address %></td>
|
<td><%= worker.address %></td>
|
||||||
<td><%= worker.phone %></td>
|
<td><%= worker.phone %></td>
|
||||||
<td><%= worker.email %></td>
|
<td><%= worker.email %></td>
|
||||||
|
|
|
@ -6,8 +6,8 @@
|
||||||
<div class="card">
|
<div class="card">
|
||||||
<div class="card-body">
|
<div class="card-body">
|
||||||
<p class="card-text">
|
<p class="card-text">
|
||||||
<strong>Name:</strong>
|
<strong>Name:</strong>
|
||||||
<%= @worker.name %>
|
<%= @worker.first_name %> <%= @worker.last_name %>
|
||||||
</p>
|
</p>
|
||||||
<p class="card-text">
|
<p class="card-text">
|
||||||
<strong>Address:</strong>
|
<strong>Address:</strong>
|
||||||
|
|
|
@ -14,7 +14,8 @@ Rails.application.routes.draw do
|
||||||
post 'link_worker'
|
post 'link_worker'
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
resources :workers
|
|
||||||
|
resources :workers
|
||||||
resources :vendors
|
resources :vendors
|
||||||
resources :employments, only: [:edit, :update, :destroy]
|
resources :employments, only: [:edit, :update, :destroy]
|
||||||
resources :employers do
|
resources :employers do
|
||||||
|
|
|
@ -0,0 +1,6 @@
|
||||||
|
class AddNameFieldsToWorkers < ActiveRecord::Migration[7.1]
|
||||||
|
def change
|
||||||
|
add_column :workers, :first_name, :string
|
||||||
|
add_column :workers, :last_name, :string
|
||||||
|
end
|
||||||
|
end
|
|
@ -10,7 +10,7 @@
|
||||||
#
|
#
|
||||||
# It's strongly recommended that you check this file into your version control system.
|
# It's strongly recommended that you check this file into your version control system.
|
||||||
|
|
||||||
ActiveRecord::Schema[7.1].define(version: 2024_01_23_042316) do
|
ActiveRecord::Schema[7.1].define(version: 2024_01_23_235419) do
|
||||||
create_table "employers", force: :cascade do |t|
|
create_table "employers", force: :cascade do |t|
|
||||||
t.string "name"
|
t.string "name"
|
||||||
t.string "address"
|
t.string "address"
|
||||||
|
@ -112,6 +112,8 @@ ActiveRecord::Schema[7.1].define(version: 2024_01_23_042316) do
|
||||||
t.string "gender"
|
t.string "gender"
|
||||||
t.datetime "created_at", null: false
|
t.datetime "created_at", null: false
|
||||||
t.datetime "updated_at", null: false
|
t.datetime "updated_at", null: false
|
||||||
|
t.string "first_name"
|
||||||
|
t.string "last_name"
|
||||||
end
|
end
|
||||||
|
|
||||||
add_foreign_key "employments", "participants"
|
add_foreign_key "employments", "participants"
|
||||||
|
|
Loading…
Reference in New Issue