87 lines
2.2 KiB
Ruby
87 lines
2.2 KiB
Ruby
class ParticipantsController < ApplicationController
|
|
before_action :set_participant, only: [:show, :edit, :update, :destroy]
|
|
|
|
def index
|
|
@participants = Participant.order(:last_name).page(params[:page]).per(10) # Adjust the number per page as needed
|
|
end
|
|
|
|
|
|
def show
|
|
end
|
|
|
|
def new
|
|
@participant = Participant.new
|
|
end
|
|
|
|
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
|
|
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
|
|
render :new
|
|
|
|
end
|
|
|
|
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(:first_name, :last_name, :address, :phone, :email, :mci, :dob, :ssn, :gender, :employer_id)
|
|
end
|
|
|
|
def employer_params_from_participant(participant)
|
|
{
|
|
name: "#{participant.first_name} #{participant.last_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
|
|
}
|
|
end
|
|
|
|
end
|