From fa0790a62123331d35d0a0042949683ff38cbef2 Mon Sep 17 00:00:00 2001 From: Bryan Date: Tue, 27 Jan 2026 15:07:51 +0400 Subject: [PATCH] modifier --- app/assets/stylesheets/application.css | 2 +- app/controllers/livres_controller.rb | 13 +++++++++++++ app/views/livres/_form.html.erb | 23 +++++++++++++++++++++++ app/views/livres/edit.html.erb | 3 +++ app/views/livres/index.html.erb | 3 ++- config/routes.rb | 2 +- 6 files changed, 43 insertions(+), 3 deletions(-) create mode 100644 app/views/livres/_form.html.erb create mode 100644 app/views/livres/edit.html.erb diff --git a/app/assets/stylesheets/application.css b/app/assets/stylesheets/application.css index fe93333..a93dc5c 100644 --- a/app/assets/stylesheets/application.css +++ b/app/assets/stylesheets/application.css @@ -7,4 +7,4 @@ * depending on specificity. * * Consider organizing styles into separate files for maintainability. - */ + */ \ No newline at end of file diff --git a/app/controllers/livres_controller.rb b/app/controllers/livres_controller.rb index b2ca5cb..4bd8115 100644 --- a/app/controllers/livres_controller.rb +++ b/app/controllers/livres_controller.rb @@ -20,6 +20,19 @@ class LivresController < ApplicationController end end + def edit + @livre = Livre.find(params[:id]) +end + +def update + @livre = Livre.find(params[:id]) + if @livre.update(livre_params) + redirect_to livres_path, notice: "Livre modifié avec succès." + else + render :edit, status: :unprocessable_entity + end +end + def destroy @livre = Livre.find(params[:id]) @livre.destroy diff --git a/app/views/livres/_form.html.erb b/app/views/livres/_form.html.erb new file mode 100644 index 0000000..ca3af93 --- /dev/null +++ b/app/views/livres/_form.html.erb @@ -0,0 +1,23 @@ +<%= form_with(model: @livre, local: true) do |form| %> +
+ <%= form.label :titre %>
+ <%= form.text_field :titre %> +
+ +
+ <%= form.label :auteur %>
+ <%= form.text_field :auteur %> +
+ +
+ <%= form.label :date_de_sortie %>
+ <%= form.date_field :date_de_sortie %> +
+ +
+ <%= form.submit "Modifier le livre" %> + +
+<% end %> + +> diff --git a/app/views/livres/edit.html.erb b/app/views/livres/edit.html.erb new file mode 100644 index 0000000..e1f5864 --- /dev/null +++ b/app/views/livres/edit.html.erb @@ -0,0 +1,3 @@ +

Modifier le livre

+ +<%= render 'form', livre: @livre %> diff --git a/app/views/livres/index.html.erb b/app/views/livres/index.html.erb index c5aec0e..04206d6 100644 --- a/app/views/livres/index.html.erb +++ b/app/views/livres/index.html.erb @@ -24,7 +24,8 @@ <%= livre.date_de_sortie %> <%= button_to "Supprimer", livre_path(livre), method: :delete, data: { confirm: "Supprimer ce livre ?" } %> - + <%= link_to "Modifier", edit_livre_path(livre), method: :edit, data: { confirm: "Modifier le livre ?" } %> + <% end %> diff --git a/config/routes.rb b/config/routes.rb index a9715b7..a7f4a3f 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,5 +1,5 @@ Rails.application.routes.draw do - resources :livres, only: [:index, :new, :create, :destroy,] + resources :livres, only: [:index, :new, :create, :destroy, :edit, :update] root "livres#index"