From bc32611a6404675c63c1bbd793d613f9906be15a Mon Sep 17 00:00:00 2001 From: Karim Bogtob Date: Sun, 26 Mar 2023 15:55:28 +0200 Subject: [PATCH] 2.3 - Creer des creatures --- app/controllers/creatures_controller.rb | 12 ++++++++++++ config/routes.rb | 1 + 2 files changed, 13 insertions(+) diff --git a/app/controllers/creatures_controller.rb b/app/controllers/creatures_controller.rb index a5303d7..3d9d0b0 100644 --- a/app/controllers/creatures_controller.rb +++ b/app/controllers/creatures_controller.rb @@ -12,4 +12,16 @@ class CreaturesController < ApplicationController rescue ActiveRecord::RecordNotFound render json: {}, status: 404 end + + def create + @creature = Creature.new(create_params) + @creature.health_points = rand(3..30) + @creature.save! + render json: @creature.as_json(only: SHOWABLE_ATTRIBUTES) + end + + private + def create_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 f3bf35a..e1393fc 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -5,6 +5,7 @@ Rails.application.routes.draw do # CRUD Créatures get '/creatures', to: 'creatures#index' get '/creatures/:id', to: 'creatures#show' + post '/creatures', to: 'creatures#create' # Defines the root path route ("/") root 'home#welcome'