From c6d4516b86a2acc7a50baaff197fd9c6e36afb62 Mon Sep 17 00:00:00 2001 From: Xero Date: Wed, 6 Mar 2024 14:02:46 -0500 Subject: [PATCH] allow filtering via params plus allow user to do a show on the name of the plant --- app/controllers/plants_controller.rb | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/app/controllers/plants_controller.rb b/app/controllers/plants_controller.rb index 096cedc..953c7ca 100644 --- a/app/controllers/plants_controller.rb +++ b/app/controllers/plants_controller.rb @@ -4,6 +4,15 @@ class PlantsController < ApplicationController # GET /plants or /plants.json def index @plants = Plant.all.includes(:genus, :family, :status).sort_by(&:name) + + case + when params[:genus].present? + @plants.select! { |plant| plant.genus.name == params[:genus] } + when params[:family].present? + @plants.select! { |plant| plant.family.name == params[:family] } + when params[:year_acquired].present? + @plants.select! { |plant| plant.year_acquired == params[:year_acquired] } + end end # GET /plants/1 or /plants/1.json @@ -63,7 +72,8 @@ def destroy private # Use callbacks to share common setup or constraints between actions. def set_plant - @plant = Plant.find(params[:id]) + @plant = Plant.find_by(name: params[:id]) + @plant ||= Plant.find(params[:id]) end # Only allow a list of trusted parameters through.