diff --git a/app/controllers/employers_controller.rb b/app/controllers/employers_controller.rb index 22a7a5c..1518fe3 100644 --- a/app/controllers/employers_controller.rb +++ b/app/controllers/employers_controller.rb @@ -44,6 +44,19 @@ class EmployersController < ApplicationController redirect_to employers_url, notice: 'Employer was successfully destroyed.' end + # GET /employers/search + # This action responds to the auto-complete AJAX requests + def search + if params[:term].present? + @employers = Employer.where("name LIKE ?", "%#{params[:term]}%") + else + @employers = Employer.none + end + + # Respond with a JSON array of employer names and IDs + render json: @employers.map { |e| { label: e.name, value: e.id } } + end + private def set_employer diff --git a/app/controllers/participants_controller.rb b/app/controllers/participants_controller.rb index cde1f19..fa26f7b 100644 --- a/app/controllers/participants_controller.rb +++ b/app/controllers/participants_controller.rb @@ -1,49 +1,84 @@ class ParticipantsController < ApplicationController - before_action :set_participant, only: [:show, :edit, :update, :destroy] + before_action :set_participant, only: [:show, :edit, :update, :destroy] - def index - @participants = Participant.all - end - - def show - end - - def new - @participant = Participant.new - end - - def create - @participant = Participant.new(participant_params) - if @participant.save - redirect_to @participant, notice: 'Participant was successfully created.' - else - render :new - end - 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(:name, :address, :phone, :email, :mci, :dob, :ssn, :gender, :employer_id) - end + def index + @participants = Participant.all end - \ No newline at end of file + + 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(: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 + } + end +end diff --git a/app/models/participant.rb b/app/models/participant.rb index 170711c..a164e70 100644 --- a/app/models/participant.rb +++ b/app/models/participant.rb @@ -1,5 +1,10 @@ class Participant < ApplicationRecord - belongs_to :employer + # This makes the association to Employer optional + belongs_to :employer, optional: true + belongs_to :worker, optional: true + + # Other associations has_and_belongs_to_many :programs has_many :workers end + diff --git a/app/views/employers/index.html.erb b/app/views/employers/index.html.erb index 534fd41..35ee8a0 100644 --- a/app/views/employers/index.html.erb +++ b/app/views/employers/index.html.erb @@ -10,10 +10,10 @@
+ Phone: + <%= @participant.phone %> +
+ ++ Email: + <%= @participant.email %> +
+ ++ MCI: + <%= @participant.mci %> +
+ ++ DOB: + <%= @participant.dob.strftime('%B, %d, %Y') if @participant.dob.present? %> +
+ + ++ SSN: + <%= @participant.ssn %> +
+ ++ Gender: + <%= @participant.gender %> +
+