diff --git a/app/controllers/employers_controller.rb b/app/controllers/employers_controller.rb index 8d46786..88ba859 100644 --- a/app/controllers/employers_controller.rb +++ b/app/controllers/employers_controller.rb @@ -48,14 +48,17 @@ class EmployersController < ApplicationController # This action responds to the auto-complete AJAX requests def search if params[:term].present? - @employers = Employer.where("name LIKE ?", "%#{params[:term]}%") + @employers = Employer.where( + "first_name LIKE :term OR last_name LIKE :term", + term: "%#{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 } } + + render json: @employers.map { |e| { label: e.full_name, value: e.id } } end + private @@ -64,6 +67,12 @@ class EmployersController < ApplicationController end def employer_params - params.require(:employer).permit(:name, :address, :phone, :email, :tin, :dob, :ssn, :gender) + params.require(:employer).permit( + :first_name, :last_name, + :address_line_1, :address_line_2, + :city, :state, :zip, + :phone, :email, :tin, :dob, :ssn, :gender + ) end + end diff --git a/app/controllers/participants_controller.rb b/app/controllers/participants_controller.rb index 8016eb7..7c10712 100644 --- a/app/controllers/participants_controller.rb +++ b/app/controllers/participants_controller.rb @@ -21,12 +21,18 @@ class ParticipantsController < ApplicationController def create @participant = Participant.new(participant_params) + Participant.transaction do if params[:is_employer] == 'yes' - employer = Employer.create!(employer_params_from_participant(@participant)) - @participant.employer = employer + employer = Employer.new(employer_params_from_participant(@participant)) + if employer.save + @participant.employer = employer + else + Rails.logger.debug "Employer creation failed: #{employer.errors.full_messages}" + raise ActiveRecord::Rollback, "Employer could not be saved" + end end - + if @participant.save redirect_to @participant, notice: 'Participant was successfully created.' else @@ -39,6 +45,7 @@ class ParticipantsController < ApplicationController render :new end + def edit end @@ -81,15 +88,24 @@ class ParticipantsController < ApplicationController def employer_params_from_participant(participant) { - name: "#{participant.first_name} #{participant.last_name}", - address: participant.address, + first_name: participant.first_name, + last_name: participant.last_name, + address_line_1: participant.address_line_1, + address_line_2: participant.address_line_2, + city: participant.city, + state: participant.state, + zip: participant.zip, phone: participant.phone, email: participant.email, - dob: participant.dob, # Mapping Date of Birth - ssn: participant.ssn, # Mapping SSN - gender: participant.gender # Mapping Gender + dob: participant.dob, + ssn: participant.ssn, + gender: participant.gender } end + + + + def employment_params params.require(:employment).permit(:worker_id, :start_date, :end_date) diff --git a/app/views/employers/edit.html.erb b/app/views/employers/edit.html.erb index 0aeab25..6b14af2 100644 --- a/app/views/employers/edit.html.erb +++ b/app/views/employers/edit.html.erb @@ -1,14 +1,13 @@
Address: - <%= [employer.address_line_1, employer.address_line_2, employer.city, employer.state, employer.zip].compact.join(', ') %> + <%= [@employer.address_line_1, @employer.address_line_2, @employer.city, @employer.state, @employer.zip].compact.join(', ') %>
@@ -48,9 +48,8 @@