diff --git a/app/assets/stylesheets/application.css b/app/assets/stylesheets/application.css index f69b2f9a..ea5eef2c 100644 --- a/app/assets/stylesheets/application.css +++ b/app/assets/stylesheets/application.css @@ -24,9 +24,34 @@ body { margin-left: 0px; font-style: normal; font-size: 110%; - flex: 1; + flex: 1; +} +/*rails forms +/* Style for form label and input fields */ +.displaytable{ + margin-top:15px; + margin-bottom:15px; +} +.form-row { + display: flex; + flex-direction: row; + /*justify-content: space-between;*/ + margin-bottom: 10px; +} +.form-row2 { + display: flex; + flex-direction: row; + /*justify-content: space-between;*/ + margin-bottom: 20px; + color:cadetblue; + font-weight:bolder; +} +.form-row label { + width: 15%;; + margin-right: 5px; } +/* end rails forms*/ .btn { background-color:#e00122; @@ -73,10 +98,9 @@ th, td { input{ line-height:1.5; font-size:100%; -border:thin black solid; +/*border:thin #e00122 solid;*/ padding:5px; -border-radius:10px; - margin:4px; +margin:4px; } diff --git a/app/controllers/employees_controller.rb b/app/controllers/employees_controller.rb index 629a84ab..4ad8f34d 100644 --- a/app/controllers/employees_controller.rb +++ b/app/controllers/employees_controller.rb @@ -35,17 +35,27 @@ def create end # PATCH/PUT /employees/1 or /employees/1.json - def update - respond_to do |format| - if @employee.update(employee_params) - format.html { redirect_to employee_url(@employee), notice: "Employee was successfully updated." } - format.json { render :show, status: :ok, location: @employee } - else - format.html { render :edit, status: :unprocessable_entity } - format.json { render json: @employee.errors, status: :unprocessable_entity } - end - end +def update + # Find the employee record that should be updated based on the `id` parameter passed in from the form +@employee = Employee.find(params[:id]) + # Get the ID of the department that the employee should be assigned to from the `department_id` parameter passed in from the form +department_id = params[:employee][:department_id] + # Find the Department record that corresponds to the ID passed in from the form +department = Department.find(department_id) + # Set the department association of the employee record to the department that was just found +@employee.department = department + # If the update of the employee attributes (e.g. first name, last name) is successful, redirect the user to the employee's show page +if @employee.update(employee_params) + redirect_to @employee + else + #display that there is a problem + flash[:error] = "Update not successful" + # If the update is not successful, re-render the edit form so that the user can fix any errors + render 'edit' end +end + + # DELETE /employees/1 or /employees/1.json def destroy @@ -64,7 +74,7 @@ def set_employee end # Only allow a list of trusted parameters through. - def employee_params - params.require(:employee).permit(:lastname, :firstname, :department_id, :pronouns, :email, :phone, :title) - end + def employee_params + params.require(:employee).permit(:lastname, :firstname, :department, :email, :phone, :title) + end end diff --git a/app/controllers/employees_controller_cp.rb b/app/controllers/employees_controller_cp.rb new file mode 100644 index 00000000..05f8156e --- /dev/null +++ b/app/controllers/employees_controller_cp.rb @@ -0,0 +1,72 @@ +class EmployeesController < ApplicationController + before_action :set_employee, only: %i[ show edit update destroy ] + + # GET /employees or /employees.json + def index + @employees = Employee.all + end + + # GET /employees/1 or /employees/1.json + def show + end + + # GET /employees/new + def new + @employee = Employee.new + end + + # GET /employees/1/edit + def edit + end + + # POST /employees or /employees.json + def create + @employee = Employee.new(employee_params) + + respond_to do |format| + if @employee.save + format.html { redirect_to employee_url(@employee), notice: "Employee was successfully created." } + format.json { render :show, status: :created, location: @employee } + else + format.html { render :new, status: :unprocessable_entity } + format.json { render json: @employee.errors, status: :unprocessable_entity } + end + end + end + + # PATCH/PUT /employees/1 or /employees/1.json + def update + respond_to do |format| + if @employee.update(employee_params) + format.html { redirect_to employee_url(@employee), notice: "Employee was successfully updated." } + format.json { render :show, status: :ok, location: @employee } + else + format.html { render :edit, status: :unprocessable_entity } + format.json { render json: @employee.errors, status: :unprocessable_entity } + end + end + end + + # DELETE /employees/1 or /employees/1.json + def destroy + @employee.destroy + + respond_to do |format| + format.html { redirect_to employees_url, notice: "Employee was successfully destroyed." } + format.json { head :no_content } + end + end + + private + # Use callbacks to share common setup or constraints between actions. + def set_employee + @employee = Employee.find(params[:id]) + end + + # Only allow a list of trusted parameters through. + def employee_params + params.require(:employee).permit(:lastname, :firstname, :department, :department_id, :department_name, :email, :phone, :title) + end + + +end diff --git a/app/controllers/employees_controllerdoesnotwork.rb b/app/controllers/employees_controllerdoesnotwork.rb new file mode 100644 index 00000000..cc921120 --- /dev/null +++ b/app/controllers/employees_controllerdoesnotwork.rb @@ -0,0 +1,74 @@ +class EmployeesController < ApplicationController + before_action :set_employee, only: %i[ show edit update destroy ] + + # GET /employees or /employees.json + def index + @employees = Employee.all + end + + # GET /employees/1 or /employees/1.json + def show + end + + # GET /employees/new + def new + @employee = Employee.new + end + + # GET /employees/1/edit + def edit + end + + # POST /employees or /employees.json + def create + @employee = Employee.new(employee_params) + department = Department.find_or_create_by(name: employee_params[:department_name]) + @employee.department = department + + respond_to do |format| + if @employee.save + format.html { redirect_to @employee, notice: 'Employee was successfully created.' } + format.json { render :show, status: :created, location: @employee } + else + format.html { render :new } + format.json { render json: @employee.errors, status: :unprocessable_entity } + end + end +end +#update +def update + respond_to do |format| + if @employee.update(employee_params) + format.html { redirect_to @employee, notice: 'Employee was successfully updated.' } + format.json { render :show, status: :ok, location: @employee } + else + format.html { render :edit } + format.json { render json: @employee.errors, status: :unprocessable_entity } + end + end +end + + # DELETE /employees/1 or /employees/1.json + def destroy + @employee.destroy + + respond_to do |format| + format.html { redirect_to employees_url, notice: "Employee was successfully destroyed." } + format.json { head :no_content } + end + end + + private + # Use callbacks to share common setup or constraints between actions. + def set_employee + @employee = Employee.find(params[:id]) + end + + # Only allow a list of trusted parameters through. + +def employee_params + params.require(:employee).permit(:lastname, :firstname, :email, :phone, :title, :department_name, :department_id) +end + + +end diff --git a/app/views/departments/index.html.erb b/app/views/departments/index.html.erb index 5d996d48..d94e34e5 100644 --- a/app/views/departments/index.html.erb +++ b/app/views/departments/index.html.erb @@ -15,7 +15,8 @@
- <% @departments.each do |department| %> + <% @departments.order(:name).each do |department| %> +Name | -Action | -|
---|---|---|
<%= employee.name %> | -<%= employee.email %> | -<%= link_to 'Edit', edit_employee_employee_path(employee) %> | -
<%= notice %>
-Lastname | Firstname | Department | -Pronouns | + +Title | Phone | @@ -17,13 +16,14 @@ | |||
---|---|---|---|---|---|---|---|---|---|
<%= employee.lastname %> | <%= employee.firstname %> | <%= employee.department.nil? ? "" : employee.department.name %> | -<%= employee.pronouns %> | -<%= employee.email %> | +<%= employee.title %> | +<%= link_to employee.email, "mailto:#{employee.email}" %> | <%= employee.phone %> | <%= link_to 'Show', employee %> | <%= link_to 'Edit', edit_employee_path(employee) %> | @@ -35,4 +35,4 @@