All associated Part, Emp, Worker and Vendor pages have been created (still need to edit look and recheck field validation and input)

This commit is contained in:
Ben 2024-01-17 01:44:36 -06:00
parent d4f7e64906
commit 9569ae50c6
9 changed files with 231 additions and 1 deletions

View File

@ -0,0 +1,47 @@
class VendorsController < ApplicationController
def index
@vendors = Vendor.all
end
def show
@vendor = Vendor.find(params[:id])
end
def new
@vendor = Vendor.new
end
def create
@vendor = Vendor.new(vendor_params)
if @vendor.save
redirect_to @vendor
else
render :new
end
end
def edit
@vendor = Vendor.find(params[:id])
end
def update
@vendor = Vendor.find(params[:id])
if @vendor.update(vendor_params)
redirect_to @vendor
else
render :edit
end
end
def destroy
@vendor = Vendor.find(params[:id])
@vendor.destroy
redirect_to vendors_path
end
private
def vendor_params
params.require(:vendor).permit(:name, :address, :phone, :email, :dba, :tin, :contact)
end
end

View File

@ -0,0 +1,2 @@
module VendorsHelper
end

94
app/views/vendors/_form.html.erb vendored Normal file
View File

@ -0,0 +1,94 @@
<%= form_with(model: vendor, local: true) do |form| %>
<% if vendor.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(vendor.errors.count, "error") %> prohibited this vendor from being saved:</h2>
<ul>
<% vendor.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= form.label :name %>
<%= form.text_field :name %>
</div>
<div class="field">
<%= form.label :address %>
<%= form.text_area :address, rows: 1, class: 'auto-expand' %>
</div>
<div class="field">
<%= form.label :phone %>
<%= form.telephone_field :phone, id: 'phone-field' %>
</div>
<div class="field">
<%= form.label :email %>
<%= form.email_field :email, id: 'email-field', required: true %>
</div>
<div class="field">
<%= form.label :dba, 'DBA' %>
<%= form.text_field :dba %>
</div>
<div class="field">
<%= form.label :tin, 'TIN' %>
<%= form.text_field :tin, maxlength: 9 %>
</div>
<div class="field">
<%= form.label :contact %>
<%= form.text_field :contact %>
</div>
<div class="actions">
<%= form.submit %>
</div>
<% end %>
<%# This is to correct phone number entry %>
<script>
document.addEventListener("DOMContentLoaded", function() {
const phoneField = document.getElementById('phone-field');
phoneField.addEventListener('input', function() {
let input = phoneField.value.replace(/\D/g, ''); // Remove non-numeric characters
const inputLength = input.length;
if (inputLength > 3 && inputLength <= 6) {
input = `(${input.slice(0, 3)}) ${input.slice(3)}`;
} else if (inputLength > 6) {
input = `(${input.slice(0, 3)}) ${input.slice(3, 6)}-${input.slice(6, 10)}`;
}
phoneField.value = input;
});
});
</script>
<%# This is for auto resizing the address field %>
<script>
document.addEventListener("DOMContentLoaded", function() {
const textareas = document.querySelectorAll('.auto-expand');
const resizeTextarea = function(el) {
el.style.height = 'auto';
el.style.height = (el.scrollHeight) + 'px';
}
textareas.forEach(textarea => {
textarea.addEventListener('input', function() {
resizeTextarea(textarea);
});
// Initial resize
resizeTextarea(textarea);
});
});
</script>

6
app/views/vendors/edit.html.erb vendored Normal file
View File

@ -0,0 +1,6 @@
<h1>Edit Vendor</h1>
<%= render 'form', vendor: @vendor %>
<%= link_to 'Cancel', vendor_path(@vendor) %> |
<%= link_to 'Back to List', vendors_path %>

36
app/views/vendors/index.html.erb vendored Normal file
View File

@ -0,0 +1,36 @@
<h1>Vendors</h1>
<table>
<thead>
<tr>
<th>Name</th>
<th>Address</th>
<th>Phone</th>
<th>Email</th>
<th>DBA</th>
<th>TIN</th>
<th>Contact</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<% @vendors.each do |vendor| %>
<tr>
<td><%= vendor.name %></td>
<td><%= vendor.address %></td>
<td><%= vendor.phone %></td>
<td><%= vendor.email %></td>
<td><%= vendor.dba %></td>
<td><%= vendor.tin %></td>
<td><%= vendor.contact %></td>
<td>
<%= link_to 'Show', vendor_path(vendor) %> |
<%= link_to 'Edit', edit_vendor_path(vendor) %> |
<%= link_to 'Destroy', vendor_path(vendor), method: :delete, data: { confirm: 'Are you sure?' } %>
</td>
</tr>
<% end %>
</tbody>
</table>
<%= link_to 'New Vendor', new_vendor_path %>

5
app/views/vendors/new.html.erb vendored Normal file
View File

@ -0,0 +1,5 @@
<h1>New Vendor</h1>
<%= render 'form', vendor: @vendor %>
<%= link_to 'Back to List', vendors_path %>

33
app/views/vendors/show.html.erb vendored Normal file
View File

@ -0,0 +1,33 @@
<h1>Vendor Details</h1>
<p>
<strong>Name:</strong>
<%= @vendor.name %>
</p>
<p>
<strong>Address:</strong>
<%= @vendor.address %>
</p>
<p>
<strong>Phone:</strong>
<%= @vendor.phone %>
</p>
<p>
<strong>Email:</strong>
<%= @vendor.email %>
</p>
<p>
<strong>DBA:</strong>
<%= @vendor.dba %>
</p>
<p>
<strong>TIN:</strong>
<%= @vendor.tin %>
</p>
<p>
<strong>Contact:</strong>
<%= @vendor.contact %>
</p>
<%= link_to 'Edit', edit_vendor_path(@vendor) %> |
<%= link_to 'Back to List', vendors_path %>

View File

@ -12,7 +12,7 @@ Rails.application.routes.draw do
resources :participants
resources :employers
resources :workers
resources :vendors

View File

@ -0,0 +1,7 @@
require "test_helper"
class VendorsControllerTest < ActionDispatch::IntegrationTest
# test "the truth" do
# assert true
# end
end