You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

48 lines
1.0 KiB

class LivresController < ApplicationController
def index
if params[:query].present?
@livres = Livre.where("titre LIKE ? OR auteur LIKE ?", "%#{params[:query]}%", "%#{params[:query]}%")
else
@livres = Livre.all
end
end
def new
@livre = Livre.new
end
def create
@livre = Livre.new(livre_params)
if @livre.save
redirect_to livres_path, notice: "Livre ajouté avec succès."
else
render :new, status: :unprocessable_entity
end
end
def edit
@livre = Livre.find(params[:id])
render :form, layout: false
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
redirect_to livres_path, notice: "Livre supprimé avec succès."
end
private
def livre_params
params.require(:livre).permit(:titre, :auteur, :date_de_sortie)
end
end