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| %> + <%= department.id %> <%= department.name %> diff --git a/app/views/employees/edit.html.erb b/app/views/employees/edit.html.erb index 18e70a44..d8c614c4 100644 --- a/app/views/employees/edit.html.erb +++ b/app/views/employees/edit.html.erb @@ -1,28 +1,55 @@

Edit Employee

<%= form_for @employee do |f| %> + + +<% if flash[:error] %> +
+ <%= flash[:error] %> +
+<% end %> + + + +
<%= f.label :firstname %> <%= f.text_field :firstname %> +
+
<%= f.label :lastname %> <%= f.text_field :lastname %> +
+
<%= f.label :email %> <%= f.text_field :email %> +
+
<%= f.label :phone %> <%= f.text_field :phone %> +
+
<%= f.label :title %> <%= f.text_field :title %> +
- <%= f.label :department %> - <%= f.collection_select :department_id, Department.all, :id, :name %> +
+ <%= f.label :Current_Department %> + <%= (@employee.department.nil? ? "None Assigned" : @employee.department.name) %> +
- <%= f.label :pronouns %> - <%= f.text_field :pronouns %> +
+ <%= f.label :Change_dept_to %> + <%= f.collection_select :department_id, Department.order(:name), :id, :name %> +
+
<%= f.submit "Save" %> +
<% end %> + <%= link_to "Back", employees_path %> diff --git a/app/views/employees/edit_employee.html.erb b/app/views/employees/edit_employee.html.erb deleted file mode 100644 index 8ec82347..00000000 --- a/app/views/employees/edit_employee.html.erb +++ /dev/null @@ -1,20 +0,0 @@ -

Edit Employees

- - - - - - - - - - - <% @employees.each do |employee| %> - - - - - - <% end %> - -
NameEmailAction
<%= employee.name %><%= employee.email %><%= link_to 'Edit', edit_employee_employee_path(employee) %>
diff --git a/app/views/employees/index.html.erb b/app/views/employees/index.html.erb index 813c3589..e7c3bea8 100644 --- a/app/views/employees/index.html.erb +++ b/app/views/employees/index.html.erb @@ -1,15 +1,14 @@

<%= notice %>

-

Employees

- - +
- + + @@ -17,13 +16,14 @@ - <% @employees.each do |employee| %> + <% @employees.order(lastname: :asc).each do |employee| %> + - - + + @@ -35,4 +35,4 @@
-<%= link_to 'New Employee', new_employee_path %> + diff --git a/app/views/employees/show.html.erb b/app/views/employees/show.html.erb index 58f9b6d9..391207b5 100644 --- a/app/views/employees/show.html.erb +++ b/app/views/employees/show.html.erb @@ -1,39 +1,36 @@

<%= notice %>

- -

- Lastname: +

Employee Detail

+
+ Last name: <%= @employee.lastname %> -

+
-

- Firstname: +

+ First name: <%= @employee.firstname %> -

+
-

+

Email: <%= @employee.email %> -

+
-

+

Phone: <%= @employee.phone %> -

+
-

+

Title: <%= @employee.title %> -

+
-

+

Department: - <%= @employee.department %> -

+ <%= @employee.department.nil? ? "" : @employee.department.name %> +
+
-

- Pronouns: - <%= @employee.pronouns %> -

+

<%= link_to 'Edit', edit_employee_path(@employee) %> | +<%= link_to 'Back', employees_path %>

-<%= link_to 'Edit', edit_employee_path(@employee) %> | -<%= link_to 'Back', employees_path %>
Lastname Firstname DepartmentPronounsTitle Email Phone
<%= 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) %>