Fixed form deletion when records are or are not dependent

This commit is contained in:
Ben 2024-02-17 03:30:49 -06:00
parent cf759a4cc7
commit 0c7d31bac0
4 changed files with 44 additions and 21 deletions

View File

@ -15,7 +15,7 @@ class FormsController < ApplicationController
def create
@form = Form.new(form_params)
if @form.save
redirect_to @form, notice: 'Form was successfully created.'
redirect_to forms_path, notice: 'Form was successfully created.'
else
render :new
end
@ -33,9 +33,16 @@ class FormsController < ApplicationController
end
def destroy
@form = Form.find(params[:id])
# Manually delete or handle associated records here
@form.onboarding_items.destroy_all
@form.forms_roles.destroy_all
@form.destroy
redirect_to forms_url, notice: 'Form was successfully destroyed.'
rescue ActiveRecord::InvalidForeignKey
redirect_to forms_url, alert: 'Form could not be deleted. There are dependent records still associated.'
end
private
def set_form

View File

@ -72,6 +72,12 @@ class WorkersController < ApplicationController
render 'show', alert: 'Unable to link participant.'
end
end
def onboarding
@worker = worker.find(params[:id])
@onboarding_items = @worker.onboarding_items.includes(:form)
end
private
def worker_params

View File

@ -6,7 +6,6 @@ class Form < ApplicationRecord
accepts_nested_attributes_for :forms_roles, allow_destroy: true
validates :name, presence: true
validates :role, inclusion: { in: %w[Participant Worker Vendor Employer] }
# Include more validations as necessary
after_save :update_onboarding_items

View File

@ -1,19 +1,30 @@
<h1><%= @form.name %></h1>
<p>
<strong>Required/Optional:</strong>
<%= @form.required ? 'Required' : 'Optional' %>
</p>
<p>
<strong>Program:</strong>
<%= @form.program %>
</p>
<p>
<strong>Who/Role:</strong>
<%= @form.role %>
</p>
<%= link_to 'Edit', edit_form_path(@form), class: 'btn btn-info' %> |
<%= link_to 'Back to Forms', forms_path, class: 'btn btn-secondary' %>
<div class="container mt-5">
<div class="row justify-content-center">
<div class="col-md-8">
<div class="card">
<div class="card-header">
<h1 class="card-title"><%= @form.name %></h1>
</div>
<div class="card-body">
<p class="card-text">
<strong>Required/Optional:</strong>
<%= @form.required ? 'Required' : 'Optional' %>
</p>
<p class="card-text">
<strong>Program:</strong>
<%= @form.program %>
</p>
<p class="card-text">
<strong>Who/Role:</strong>
<%# Assuming you have adjusted @form.role to handle multiple roles %>
<%= @form.roles.map(&:name).join(', ') %>
</p>
</div>
<div class="card-footer text-muted d-flex justify-content-between">
<%= link_to 'Edit', edit_form_path(@form), class: 'btn btn-info' %>
<%= link_to 'Back to Forms', forms_path, class: 'btn btn-secondary' %>
</div>
</div>
</div>
</div>
</div>