From 3f4df1c8e524c1d4a3221511139d8dd75b078fff Mon Sep 17 00:00:00 2001 From: Karim Bogtob Date: Sun, 26 Mar 2023 15:57:38 +0200 Subject: [PATCH] 2.4 - Mettre a jour une creature --- app/controllers/creatures_controller.rb | 12 ++++++++++-- config/routes.rb | 1 + 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/app/controllers/creatures_controller.rb b/app/controllers/creatures_controller.rb index 3d9d0b0..11bc911 100644 --- a/app/controllers/creatures_controller.rb +++ b/app/controllers/creatures_controller.rb @@ -14,14 +14,22 @@ class CreaturesController < ApplicationController end def create - @creature = Creature.new(create_params) + @creature = Creature.new(create_or_update_params) @creature.health_points = rand(3..30) @creature.save! render json: @creature.as_json(only: SHOWABLE_ATTRIBUTES) end + def update + @creature = Creature.find(params[:id]) + @creature.update!(create_or_update_params) + render json: @creature.as_json(only: SHOWABLE_ATTRIBUTES) + rescue ActiveRecord::RecordNotFound + render json: {}, status: 404 + end + private - def create_params + def create_or_update_params params.require(:creature).permit(:name) end end \ No newline at end of file diff --git a/config/routes.rb b/config/routes.rb index e1393fc..7162598 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -6,6 +6,7 @@ Rails.application.routes.draw do get '/creatures', to: 'creatures#index' get '/creatures/:id', to: 'creatures#show' post '/creatures', to: 'creatures#create' + put '/creatures/:id', to: 'creatures#update' # Defines the root path route ("/") root 'home#welcome'