I am working on a Staffing app for managers at Fedex. Currently you can choose a date and then fill that date with employees’ records which include the attributes “Name, Employee Number, and Comments”. The employee record is an association to (belongs to) StaffDate and Template.
What I would like to do is be able to save the employee records that are currently on the screen to a template, name that template. Then after choosing a new date you have the option of picking a saved list (template) of employees to populate the list and then be edited if needed. Just so the manager’s dont have to type in 40+ names night after night.
Below are the controllers, model, and routes for my StaffDate, Employee, and Template. Also worth mentioning is that the Template record as an attribute called Employees which is an array. Whereas the StaffDate, which holds the exact same information about the employees, does not.
Currently I do not get any errors but when I click the ‘save template’ button, a new Template is not created. I can provide more information and a repo if needed. It seems so simple but I have been stuck for days and I can’t find ANYTHING on this subject so it will be a good address.
class TemplatesController < ApplicationController
before_action :set_staff_date
def index
@templates = Templates.all
end
def save_temp
employees = @staff_date.employees
@template = current_user.templates.create!(template_params)
@template.employees << ([employees: employees])
@template.save
end
private
def set_staff_date
@staff_date = current_user.staff_dates.find(params[:id])
end
def template_params
params.permit(:name, :employees)
end
end
class EmployeesController < ApplicationController
before_action :set_staff_date
before_action :set_employee, only: %i[ show edit update destroy ]
def new
@employee = @staff_date.employees.build
end
def create
@employee = @staff_date.employees.build(employee_params)
if @employee.save
respond_to do |format|
format.html { redirect_to staff_date(@staff_date), notice: "Item was successfully created." }
format.turbo_stream { flash.now[:notice] = "Item was successfully created." }
end
else
render :new, status: :unprocessable_entity
end
end
def edit
end
def update
if @employee.update(employee_params)
respond_to do |format|
format.html { redirect_to line_staff_date_path(@employee), notice: "Item was successfully updated." }
format.turbo_stream { flash.now[:notice] = "Item was successfully updated." }
end
else
render :edit, status: :unprocessable_entity
end
end
def destroy
@employee.destroy
respond_to do |format|
format.html { redirect_to staff_date_path(@staff_date), notice: "Date was successfully destroyed." }
format.turbo_stream { flash.now[:notice] = "Date was successfully destroyed." }
end
end
private
def set_employee
@employee = @staff_date.employees.find(params[:id])
end
def employee_params
params.require(:employee).permit(:name, :employee_number, :comment)
end
def set_staff_date
@staff_date = current_user.staff_dates.find(params[:staff_date_id])
end
def set_template
@template = current_user.templates.find(params[:template_id])
end
end
class StaffDatesController < ApplicationController
before_action :set_staff_date, only: %i[ show edit update destroy send_email]
def send_email
StaffMailer.with(employees: @staff_date.employees).send_staff(current_user).deliver_now
# flash.now[:notice] = "Staff was successfully sent"
head :ok
end
# GET /staff_dates or /staff_dates.json
def index
@staff_dates = current_user.staff_dates
end
def show
end
# GET /staff_dates/new
def new
@staff_date = current_user.staff_dates.new
end
# GET /staff_dates/1/edit
def edit
end
# POST /staff_dates or /staff_dates.json
def create
@staff_date = current_user.staff_dates.new(staff_date_params)
if @staff_date.save
respond_to do |format|
format.html { redirect_to staff_dates_path, notice: "Date was successfully created." }
format.turbo_stream { flash.now[:notice] = "Date was successfully created." }
end
else
render :new, status: :unprocessable_entity
end
end
def update
if @staff_date.update(staff_date_params)
respond_to do |format|
format.html { redirect_to staff_dates_path, notice: "Date was successfully updated." }
format.turbo_stream { flash.now[:notice] = "Date was successfully updated." }
end
else
render :edit, status: :unprocessable_entity
end
end
# DELETE /staff_dates/1 or /staff_dates/1.json
def destroy
@staff_date.destroy
respond_to do |format|
format.html { redirect_to @staff_date, notice: "Date was successfully destroyed." }
format.turbo_stream { flash.now[:notice] = "Date was successfully destroyed." }
end
end
private
def set_staff_date
@staff_date = current_user.staff_dates.find(params[:id])
end
def staff_date_params
params.require(:staff_date).permit(:date)
end
def employee_params
params.require(:employee).permit(:name, :employee_number, :comment)
end
end
class Template < ApplicationRecord
belongs_to :user
end
require 'sidekiq/web'
Rails.application.routes.draw do
get 'templates/index'
draw :madmin
get '/privacy', to: 'home#privacy'
get '/terms', to: 'home#terms'
authenticate :user, lambda { |u| u.admin? } do
mount Sidekiq::Web => '/sidekiq'
namespace :madmin do
resources :impersonates do
post :impersonate, on: :member
post :stop_impersonating, on: :collection
end
end
end
resources :notifications, only: [:index]
resources :announcements, only: [:index]
devise_for :users, controllers: { omniauth_callbacks: "users/omniauth_callbacks" }
root to: 'home#index'
resources :staff_dates do
resources :employees, except: [:index, :show]
post "send_email", on: :member
end
resources :templates do
post "save_temp", on: :member
end
end
class Employee < ApplicationRecord
belongs_to :staff_date, optional: true
belongs_to :template, optional: true
validates :name, presence: true
validates :employee_number, presence: true, numericality: { only_integer: true, greater_than: 0 }
end
class StaffDate < ApplicationRecord
has_many :employees, dependent: :destroy
belongs_to :user
validates :date, presence: true
scope :ordered, -> { order(date: asc) }
def previous_date
staff_dates.ordered.where("date < ?", date).last
end
broadcasts_to ->(staff_date) { [staff_date, "staff_dates"] }, inserts_by: :prepend
end