diff --git a/app/controllers/participants_controller.rb b/app/controllers/participants_controller.rb index 836041c..861e3a7 100644 --- a/app/controllers/participants_controller.rb +++ b/app/controllers/participants_controller.rb @@ -8,7 +8,10 @@ class ParticipantsController < ApplicationController def show + @participant = Participant.includes(:employments).find(params[:id]) + @workers = @participant.workers # Fetch associated workers end + def new @participant = Participant.new @@ -16,34 +19,22 @@ class ParticipantsController < ApplicationController def create @participant = Participant.new(participant_params) - - begin - Participant.transaction do - if params[:is_employer] == 'yes' - employer = Employer.create!(employer_params_from_participant(@participant)) - @participant.employer = employer - end - - if @participant.save - redirect_to @participant, notice: 'Participant was successfully created.' - else - raise ActiveRecord::Rollback, "Participant could not be saved" - end + Participant.transaction do + if params[:is_employer] == 'yes' + employer = Employer.create!(employer_params_from_participant(@participant)) + @participant.employer = employer + end + + if @participant.save + redirect_to @participant, notice: 'Participant was successfully created.' + else + raise ActiveRecord::Rollback, "Participant could not be saved" end - rescue ActiveRecord::RecordInvalid => e - # Log the error and set a flash message for the user - Rails.logger.error("Participant creation failed: #{e.message}") - flash.now[:alert] = "Error: #{e.message}" - render :new end - - - - rescue => e - puts "Error: #{e.message}" # Log the error message - flash.now[:alert] = "Error: #{e.message}" # Optionally, display the error message to the user + rescue ActiveRecord::RecordInvalid => e + Rails.logger.error("Participant creation failed: #{e.message}") + flash.now[:alert] = "Error: #{e.message}" render :new - end def edit @@ -62,6 +53,17 @@ class ParticipantsController < ApplicationController redirect_to participants_url, notice: 'Participant was successfully destroyed.' end + def link_worker + @participant = Participant.find(params[:id]) + employment = @participant.employments.new(employment_params) + if employment.save + redirect_to @participant, notice: 'Worker was successfully linked.' + else + # Assuming you have a show or edit page to render + render 'show', alert: 'Failed to link worker.' + end + end + private def set_participant @@ -83,5 +85,8 @@ class ParticipantsController < ApplicationController gender: participant.gender # Mapping Gender } end - + + def employment_params + params.require(:employment).permit(:worker_id, :start_date, :end_date) + end end diff --git a/app/models/employment.rb b/app/models/employment.rb new file mode 100644 index 0000000..1846279 --- /dev/null +++ b/app/models/employment.rb @@ -0,0 +1,4 @@ +class Employment < ApplicationRecord + belongs_to :worker + belongs_to :participant +end diff --git a/app/models/participant.rb b/app/models/participant.rb index 27b3a6a..3131413 100644 --- a/app/models/participant.rb +++ b/app/models/participant.rb @@ -1,12 +1,13 @@ class Participant < ApplicationRecord - # This makes the association to Employer optional + # Associations belongs_to :employer, optional: true - belongs_to :worker, optional: true + has_many :employments + has_many :workers, through: :employments + + # Validations validates :first_name, presence: true validates :last_name, presence: true # Other associations has_and_belongs_to_many :programs - has_many :workers end - diff --git a/app/models/worker.rb b/app/models/worker.rb index de3f1cb..930f3c8 100644 --- a/app/models/worker.rb +++ b/app/models/worker.rb @@ -1,6 +1,7 @@ class Worker < ApplicationRecord # One-to-many relationship with Participants - has_many :participants + has_many :participants, through: :employments + has_many :employments # Many-to-many relationship with Employers through Participants has_many :employers, through: :participants diff --git a/app/views/devise/sessions/new.html.erb b/app/views/devise/sessions/new.html.erb index d037815..27f726b 100644 --- a/app/views/devise/sessions/new.html.erb +++ b/app/views/devise/sessions/new.html.erb @@ -22,7 +22,7 @@ <% end %>
Worker Name | +Start Date | +End Date | +
---|---|---|
<%= employment.worker.name %> | +<%= employment.start_date %> | +<%= employment.end_date %> | +