2024-01-16 23:10:18 -06:00
|
|
|
class ParticipantsController < ApplicationController
|
2024-01-19 23:38:15 -06:00
|
|
|
before_action :set_participant, only: [:show, :edit, :update, :destroy]
|
2024-01-16 23:10:18 -06:00
|
|
|
|
2024-01-19 23:38:15 -06:00
|
|
|
def index
|
|
|
|
@participants = Participant.all
|
|
|
|
end
|
|
|
|
|
|
|
|
def show
|
|
|
|
end
|
|
|
|
|
|
|
|
def new
|
|
|
|
@participant = Participant.new
|
|
|
|
end
|
|
|
|
|
|
|
|
def create
|
|
|
|
@participant = Participant.new(participant_params)
|
2024-01-16 23:10:18 -06:00
|
|
|
|
2024-01-19 23:38:15 -06:00
|
|
|
begin
|
|
|
|
Participant.transaction do
|
|
|
|
if params[:is_employer] == 'yes'
|
|
|
|
employer = Employer.create!(employer_params_from_participant(@participant))
|
|
|
|
@participant.employer = employer
|
|
|
|
end
|
2024-01-16 23:10:18 -06:00
|
|
|
|
2024-01-19 23:38:15 -06:00
|
|
|
if @participant.save
|
|
|
|
redirect_to @participant, notice: 'Participant was successfully created.'
|
|
|
|
else
|
|
|
|
raise ActiveRecord::Rollback, "Participant could not be saved"
|
|
|
|
end
|
2024-01-16 23:10:18 -06:00
|
|
|
end
|
2024-01-19 23:38:15 -06:00
|
|
|
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
|
2024-01-16 23:10:18 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
|
2024-01-19 23:38:15 -06:00
|
|
|
|
|
|
|
rescue => e
|
|
|
|
puts "Error: #{e.message}" # Log the error message
|
|
|
|
flash.now[:alert] = "Error: #{e.message}" # Optionally, display the error message to the user
|
|
|
|
render :new
|
2024-01-16 23:10:18 -06:00
|
|
|
|
2024-01-19 23:38:15 -06:00
|
|
|
end
|
2024-01-16 23:10:18 -06:00
|
|
|
|
2024-01-19 23:38:15 -06:00
|
|
|
def edit
|
|
|
|
end
|
|
|
|
|
|
|
|
def update
|
|
|
|
if @participant.update(participant_params)
|
|
|
|
redirect_to @participant, notice: 'Participant was successfully updated.'
|
|
|
|
else
|
|
|
|
render :edit
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def destroy
|
|
|
|
@participant.destroy
|
|
|
|
redirect_to participants_url, notice: 'Participant was successfully destroyed.'
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def set_participant
|
|
|
|
@participant = Participant.find(params[:id])
|
|
|
|
end
|
|
|
|
|
|
|
|
def participant_params
|
|
|
|
params.require(:participant).permit(:name, :address, :phone, :email, :mci, :dob, :ssn, :gender, :employer_id)
|
|
|
|
end
|
|
|
|
|
|
|
|
def employer_params_from_participant(participant)
|
|
|
|
{
|
|
|
|
name: participant.name,
|
|
|
|
address: participant.address,
|
|
|
|
phone: participant.phone,
|
|
|
|
email: participant.email,
|
|
|
|
dob: participant.dob, # Mapping Date of Birth
|
|
|
|
ssn: participant.ssn, # Mapping SSN
|
|
|
|
gender: participant.gender # Mapping Gender
|
|
|
|
}
|
2024-01-16 23:10:18 -06:00
|
|
|
end
|
2024-01-19 23:38:15 -06:00
|
|
|
end
|