From 7c3e5d79bb17339f7dda3bcf92534442ad6c22ec Mon Sep 17 00:00:00 2001 From: Karim Bogtob Date: Sun, 26 Mar 2023 15:51:13 +0200 Subject: [PATCH] 2.1 - Montrer une creature --- app/controllers/creatures_controller.rb | 10 ++++++++++ config/routes.rb | 3 +++ 2 files changed, 13 insertions(+) create mode 100644 app/controllers/creatures_controller.rb diff --git a/app/controllers/creatures_controller.rb b/app/controllers/creatures_controller.rb new file mode 100644 index 0000000..f3ff865 --- /dev/null +++ b/app/controllers/creatures_controller.rb @@ -0,0 +1,10 @@ +class CreaturesController < ApplicationController + SHOWABLE_ATTRIBUTES = [:id, :name, :health_points] + + def show + @creature = Creature.find(params[:id]) + render json: @creature.as_json(only: SHOWABLE_ATTRIBUTES) + rescue ActiveRecord::RecordNotFound + render json: {}, status: 404 + end +end \ No newline at end of file diff --git a/config/routes.rb b/config/routes.rb index 781608c..7638f10 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -2,6 +2,9 @@ Rails.application.routes.draw do # Define your application routes per the DSL in https://guides.rubyonrails.org/routing.html get '/dice-rolls/:dice_type/(:rolls_count)', to: "dice_rolls#rolls" + # CRUD Créatures + get '/creatures/:id', to: 'creatures#show' + # Defines the root path route ("/") root 'home#welcome' end