From 3447908626a1d56307734c8aab1475294d7265ca Mon Sep 17 00:00:00 2001 From: Selwyn Ang Date: Sun, 28 Jul 2024 16:56:51 +0800 Subject: [PATCH] Add filters --- app/controllers/admin/products_controller.rb | 6 +++++- app/controllers/categories_controller.rb | 8 ++++++++ app/views/admin/products/index.html.erb | 10 ++++++++++ app/views/categories/show.html.erb | 11 +++++++++++ 4 files changed, 34 insertions(+), 1 deletion(-) diff --git a/app/controllers/admin/products_controller.rb b/app/controllers/admin/products_controller.rb index 25785ee..e0ad3ef 100644 --- a/app/controllers/admin/products_controller.rb +++ b/app/controllers/admin/products_controller.rb @@ -3,7 +3,11 @@ class Admin::ProductsController < AdminController # GET /admin/products or /admin/products.json def index - @admin_products_pagy, @admin_products = pagy(Product.all) + if params[:query].present? + @admin_products_pagy, @admin_products = pagy(Product.where("name LIKE ?", "%#{params[:query]}%")) + else + @admin_products_pagy, @admin_products = pagy(Product.all) + end end # GET /admin/products/1 or /admin/products/1.json diff --git a/app/controllers/categories_controller.rb b/app/controllers/categories_controller.rb index c18fac7..34094ea 100644 --- a/app/controllers/categories_controller.rb +++ b/app/controllers/categories_controller.rb @@ -2,5 +2,13 @@ class CategoriesController < ApplicationController def show @category = Category.find(params[:id]) @products = @category.products + + if params[:max].present? + @products = @products.where("price <= ?", params[:max]) + end + if params[:min].present? + @products = @products.where("price >= ?", params[:min]) + end + end end diff --git a/app/views/admin/products/index.html.erb b/app/views/admin/products/index.html.erb index b4609b5..5ab2d0e 100644 --- a/app/views/admin/products/index.html.erb +++ b/app/views/admin/products/index.html.erb @@ -10,6 +10,16 @@ <%= link_to "New product", new_admin_product_path, class: "rounded-lg py-3 px-5 bg-blue-600 text-white block font-medium" %> +
+ <%= form_with url: admin_products_path, method: :get, class: "flex" do |form| %> + <%= form.text_field :query, placeholder: "Search", class: "rounded border-gray-500 text-gray-800" %> + <%= form.submit "Submit", class: "cursor-pointer bg-gray-500 ml-2 hover:bg-gray-600 text-white py-2 px-4 rounded" %> + <% end %> + <%= form_with url: admin_products_path, method: :get, class: 'flex' do |form| %> + <%= form.submit "Clear", class: "cursor-pointer bg-gray-500 ml-0 md:ml-2 mt-2 md:mt-0 hover:bg-gray-600 text-white py-2 px-4 rounded" %> + <% end %> +
+
diff --git a/app/views/categories/show.html.erb b/app/views/categories/show.html.erb index 471f420..0ad960f 100644 --- a/app/views/categories/show.html.erb +++ b/app/views/categories/show.html.erb @@ -1,5 +1,16 @@

Category Show Page

+
+

Filter

+ <%= form_with url: category_path(@category), method: :get, class: "flex flex-col" do |form| %> + <%= form.number_field :min, placeholder: "Min Price", class: "border border-gray-800 rounded-md p-2 w-32" %> + <%= form.number_field :max, placeholder: "Max Price", class: "border border-gray-800 rounded-md p-2 mt-2 w-32" %> + <%= form.submit "Filter", class: "bg-gray-800 text-white rounded-md p-2 mt-2 cursor-pointer" %> + <% end %> + <%= form_with url: category_path(@category), method: :get, class: "flex flex-col" do |form| %> + <%= form.submit "Clear", class: "bg-gray-800 text-white rounded-md p-2 mt-2 cursor-pointer" %> + <% end %> +
<% if @products.empty? %>

No products found