diff --git a/app/controllers/participants_controller.rb b/app/controllers/participants_controller.rb index 8f27cb8..100b5bc 100644 --- a/app/controllers/participants_controller.rb +++ b/app/controllers/participants_controller.rb @@ -11,6 +11,8 @@ class ParticipantsController < ApplicationController @participant = Participant.includes(:employments).find(params[:id]) @workers = @participant.workers # Fetch associated workers @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 diff --git a/app/controllers/workers_controller.rb b/app/controllers/workers_controller.rb index 2fe857c..1c37edc 100644 --- a/app/controllers/workers_controller.rb +++ b/app/controllers/workers_controller.rb @@ -41,7 +41,8 @@ class WorkersController < ApplicationController private 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 \ No newline at end of file diff --git a/app/models/worker.rb b/app/models/worker.rb index 930f3c8..8786d42 100644 --- a/app/models/worker.rb +++ b/app/models/worker.rb @@ -5,4 +5,13 @@ class Worker < ApplicationRecord # Many-to-many relationship with 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 diff --git a/app/views/employments/edit.html.erb b/app/views/employments/edit.html.erb index d122163..4486de5 100644 --- a/app/views/employments/edit.html.erb +++ b/app/views/employments/edit.html.erb @@ -14,4 +14,6 @@ <%= form.submit "Update Employment" %> <% 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' %> diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index ed08b44..e3e2c3b 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -10,7 +10,6 @@ <%= javascript_pack_tag 'application', 'data-turbolinks-track': 'reload' %> <%= javascript_include_tag 'rails-ujs', 'data-turbolinks-track': 'reload' %> - @@ -44,9 +43,7 @@ <% end %> - - - + diff --git a/app/views/participants/show.html.erb b/app/views/participants/show.html.erb index 1c5e2c6..0f310be 100644 --- a/app/views/participants/show.html.erb +++ b/app/views/participants/show.html.erb @@ -72,18 +72,18 @@ - <% @participant.employments.each do |employment| %> + <% @employments.each do |employment| %> - <%= employment.worker&.name || 'No Worker Assigned' %> + <%= employment.worker&.full_name || 'No Worker Assigned' %> <%= employment.start_date.strftime('%B %d, %Y') if employment.start_date.present? %> <%= employment.end_date.strftime('%B %d, %Y') if employment.end_date.present? %> - <%= link_to edit_employment_path(employment), class: 'btn btn-sm btn-warning' do %> - - <% end %> - <%= link_to employment, method: :delete, data: { confirm: 'Are you sure?' }, class: 'btn btn-sm btn-danger' do %> - - <% end %> + <%= link_to worker_path(employment.worker), class: 'btn btn-sm btn-secondary' do %> + + <% end %> + <%= link_to edit_employment_path(employment), class: 'btn btn-sm btn-warning' do %> + + <% end %> <% end %> @@ -91,10 +91,11 @@ + <%= form_with(model: [@participant, @employment], url: link_worker_participant_path(@participant), method: :post, class: 'row g-3') do |form| %>
<%= 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' } %>
@@ -111,4 +112,3 @@ <%= form.submit "Link Worker", class: 'btn btn-dark' %>
<% end %> - diff --git a/app/views/workers/_form.html.erb b/app/views/workers/_form.html.erb index 8483334..429336e 100644 --- a/app/views/workers/_form.html.erb +++ b/app/views/workers/_form.html.erb @@ -11,8 +11,13 @@ <% end %>
- <%= form.label :name, class: 'form-label' %> - <%= form.text_field :name, class: 'form-control' %> + <%= form.label :first_name, class: 'form-label' %> + <%= form.text_field :first_name, class: 'form-control' %> +
+ +
+ <%= form.label :last_name, class: 'form-label' %> + <%= form.text_field :last_name, class: 'form-control' %>
diff --git a/app/views/workers/index.html.erb b/app/views/workers/index.html.erb index aaded73..81b24b4 100644 --- a/app/views/workers/index.html.erb +++ b/app/views/workers/index.html.erb @@ -8,7 +8,8 @@ - + + @@ -19,7 +20,8 @@ <% @workers.each do |worker| %> - + + diff --git a/app/views/workers/show.html.erb b/app/views/workers/show.html.erb index 185a786..23114dd 100644 --- a/app/views/workers/show.html.erb +++ b/app/views/workers/show.html.erb @@ -6,8 +6,8 @@

- Name: - <%= @worker.name %> + Name: + <%= @worker.first_name %> <%= @worker.last_name %>

Address: diff --git a/config/routes.rb b/config/routes.rb index bb519c8..6f4c2c2 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -14,7 +14,8 @@ Rails.application.routes.draw do post 'link_worker' end end - resources :workers + + resources :workers resources :vendors resources :employments, only: [:edit, :update, :destroy] resources :employers do diff --git a/db/migrate/20240123235419_add_name_fields_to_workers.rb b/db/migrate/20240123235419_add_name_fields_to_workers.rb new file mode 100644 index 0000000..8ada167 --- /dev/null +++ b/db/migrate/20240123235419_add_name_fields_to_workers.rb @@ -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 diff --git a/db/schema.rb b/db/schema.rb index e778d6b..0e918c9 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # 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| t.string "name" t.string "address" @@ -112,6 +112,8 @@ ActiveRecord::Schema[7.1].define(version: 2024_01_23_042316) do t.string "gender" t.datetime "created_at", null: false t.datetime "updated_at", null: false + t.string "first_name" + t.string "last_name" end add_foreign_key "employments", "participants"

NameFirst NameLast Name Address Phone Email
<%= worker.name %><%= worker.first_name %><%= worker.last_name %> <%= worker.address %> <%= worker.phone %> <%= worker.email %>