Skip to content

Commit

Permalink
allow filtering via params plus allow user to do a show on the name of
Browse files Browse the repository at this point in the history
the plant
  • Loading branch information
one-m1nd committed Mar 6, 2024
1 parent 3237285 commit c6d4516
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion app/controllers/plants_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit c6d4516

Please sign in to comment.