From 1498c094957caf3ee7b7f7100e55224249c6f06c Mon Sep 17 00:00:00 2001 From: Xuanhuy218972 Date: Wed, 3 Dec 2025 16:53:38 +0700 Subject: [PATCH 1/3] Add destroy method for products --- app/controllers/products_controller.rb | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/app/controllers/products_controller.rb b/app/controllers/products_controller.rb index 2c6eb11..7332dda 100644 --- a/app/controllers/products_controller.rb +++ b/app/controllers/products_controller.rb @@ -23,8 +23,34 @@ def create end end + def destroy + return require_authentication unless current_user + + return product_not_found unless find_product + + product_name = @product.name + request.session['flash'] = if @product.destroy + "Product '#{product_name}' deleted" + else + 'Delete failed' + end + + [[], 302, { 'location' => '/products' }] + end + + private + def find_product + product_id = request.params['id'] + @product = Product.find_by(id: product_id) + end + + def product_not_found + request.session['flash'] = 'Product not found' + [[], 302, { 'location' => '/products' }] + end + def product_params request.params.slice( 'name', From 3550cf4a8977e3b4c07d4519b48e6fb6102cf060 Mon Sep 17 00:00:00 2001 From: Xuanhuy218972 Date: Wed, 3 Dec 2025 16:54:30 +0700 Subject: [PATCH 2/3] Add delete route for products --- config.ru | 1 + 1 file changed, 1 insertion(+) diff --git a/config.ru b/config.ru index f24c92b..063537d 100644 --- a/config.ru +++ b/config.ru @@ -34,6 +34,7 @@ use Frack::Router do get '/products' => 'products#index' get '/products/new' => 'products#new' post '/products' => 'products#create' + delete '/products' => 'products#destroy' end use OTR::ActiveRecord::ConnectionManagement From c9cee39e1cb3e493441c80516aec657e61be9771 Mon Sep 17 00:00:00 2001 From: Xuanhuy218972 Date: Wed, 3 Dec 2025 17:01:39 +0700 Subject: [PATCH 3/3] Add bootstrap for delete products --- app/views/products/index.html.erb | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/app/views/products/index.html.erb b/app/views/products/index.html.erb index a826b82..9bfd36f 100644 --- a/app/views/products/index.html.erb +++ b/app/views/products/index.html.erb @@ -22,6 +22,7 @@ Year Price Created At + Delete @@ -66,6 +67,36 @@ <%= p.created_at.strftime("%m/%d/%Y") %> + +
+ +
+