obdev/app/controllers/vendors_controller.rb

72 lines
1.4 KiB
Ruby
Raw Normal View History

class VendorsController < ApplicationController
def index
@vendors = Vendor.order(:name).page(params[:page]).per(5) # Adjust the number per page as needed
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
def search
if params[:term].present?
@vendors = Vendor.where("name LIKE ?", "%#{params[:term]}%")
else
@vendors = Vendor.none
end
# Respond with a JSON array of vendor names and IDs
render json: @vendors.map { |vendor| { label: vendor.name, value: vendor.id } }
end
private
def vendor_params
params.require(:vendor).permit(
:name,
:address_line_1,
:address_line_2,
:city,
:state,
:zip,
:phone,
:email,
:dba,
:tin,
:contact
)
end
end