From d80cff471f34c91e12c7765e41109966ee1447c5 Mon Sep 17 00:00:00 2001 From: JonathanPreminger Date: Thu, 14 Mar 2019 10:23:11 +0100 Subject: [PATCH 1/2] Crud around template --- app/controllers/templates_controller.rb | 69 +++++++++++++++++++++++++ app/views/shared/_header.html.erb | 5 ++ app/views/templates/_form.html.erb | 29 +++++++++++ app/views/templates/_template.html.erb | 6 +++ app/views/templates/edit.html.erb | 21 ++++++++ app/views/templates/index.html.erb | 36 +++++++++++++ app/views/templates/new.html.erb | 20 +++++++ app/views/templates/show.html.erb | 31 +++++++++++ config/routes.rb | 3 ++ 9 files changed, 220 insertions(+) create mode 100644 app/controllers/templates_controller.rb create mode 100644 app/views/templates/_form.html.erb create mode 100644 app/views/templates/_template.html.erb create mode 100644 app/views/templates/edit.html.erb create mode 100644 app/views/templates/index.html.erb create mode 100644 app/views/templates/new.html.erb create mode 100644 app/views/templates/show.html.erb diff --git a/app/controllers/templates_controller.rb b/app/controllers/templates_controller.rb new file mode 100644 index 0000000..7eb3f78 --- /dev/null +++ b/app/controllers/templates_controller.rb @@ -0,0 +1,69 @@ +# frozen_string_literal: true + +class TemplatesController < ApplicationController + before_action :set_template, only: %i[show edit update destroy] + + # GET /templates + # GET /templates.json + def index + @templates = Template.order(updated_at: :desc).paginate(page: params[:page]) + end + + # GET /templates/1 + # GET /templates/1.json + def show; end + + # GET /templates/new + def new + @template = Template.new + end + + # GET /templates/1/edit + def edit; end + + # POST /templates + def create + @template = Template.new(template_params) + if @template.save + redirect_to @template, notice: 'Template was successfully created.' + else + render :new + end + end + + # PATCH/PUT /templates/1 + # PATCH/PUT /templates/1.json + def update + puts "in update, params : #{template_params}" + if @template.update(template_params) + redirect_to @template, notice: 'Template was successfully updated.' + else + render :edit + end + end + + # DELETE /templates/1 + def destroy + @template.destroy + redirect_to templates_url, notice: 'Template was successfully destroyed.' + end + + # GET /templates/1/duplicate + def duplicate + original = Template.find(params[:template_id]) + @template = original.dup + render 'new' + end + + private + + # Use callbacks to share common setup or constraints between actions. + def set_template + @template = Template.find(params[:id]) + end + + # Never trust parameters from the scary internet, only allow the white list through. + def template_params + params.require(:template).permit(:title, :description, :date, :period, { skill_ids: [] }, student_ids: []) + end +end diff --git a/app/views/shared/_header.html.erb b/app/views/shared/_header.html.erb index baad085..1a938fd 100644 --- a/app/views/shared/_header.html.erb +++ b/app/views/shared/_header.html.erb @@ -34,6 +34,11 @@ Liste des séances + diff --git a/app/views/templates/_form.html.erb b/app/views/templates/_form.html.erb new file mode 100644 index 0000000..6650429 --- /dev/null +++ b/app/views/templates/_form.html.erb @@ -0,0 +1,29 @@ +
+<%= form_with(model: template, local: true) do |form| %> + <% if template.errors.any? %> +
+

<%= pluralize(template.errors.count, "error") %> prohibited this template from being saved:

+ +
    + <% template.errors.full_messages.each do |message| %> +
  • <%= message %>
  • + <% end %> +
+
+ <% end %> + +
+ <%= form.label :title %> + <%= form.text_field :title, class: "ml-2 pl-2 form-control" %> +
+ +
+ <%= form.label :description %> + <%= form.text_area :description, class: "ml-2 pl-2 form-control" %> +
+ +
+ <%= form.submit class: 'btn btn-primary btn-lg mt-3' %> +
+<% end %> +
diff --git a/app/views/templates/_template.html.erb b/app/views/templates/_template.html.erb new file mode 100644 index 0000000..4928f71 --- /dev/null +++ b/app/views/templates/_template.html.erb @@ -0,0 +1,6 @@ + + <%= template.title %> + <%= link_to "Voir", template_path(template.id), class: "btn btn-sm btn-outline-primary" %> + <%= link_to 'Dupliquer', template_duplicate_path(template) %> + <%= link_to 'Supprimer', template, method: :delete, data: { confirm: 'Cette action est irréversible, êtes-vous sûr ?' } %> + diff --git a/app/views/templates/edit.html.erb b/app/views/templates/edit.html.erb new file mode 100644 index 0000000..8743b95 --- /dev/null +++ b/app/views/templates/edit.html.erb @@ -0,0 +1,21 @@ +
+
+
+
+
+
+

Editer le template

+
+ <%= render 'form', template: @template %> + + +
+ +
+
+ <%= link_to 'Show', @template, class:"mr-3" %> + <%= link_to 'Back', templates_path %> +
+
+
+
diff --git a/app/views/templates/index.html.erb b/app/views/templates/index.html.erb new file mode 100644 index 0000000..195a5c8 --- /dev/null +++ b/app/views/templates/index.html.erb @@ -0,0 +1,36 @@ +
+
+
+
+
+
+

Liste des templates

+
+ <%= link_to new_template_path, class: "btn btn-primary btn-lg" do %> + Programmer un nouveau template + <% end %> +
+
+
+ + + + + + + + + + + + + <%= render @templates %> + +
IntituléDatePériode
+ <%= will_paginate @templates, list_classes: %w(pagination justify-content-center) %> +
+
+
+
+
+
diff --git a/app/views/templates/new.html.erb b/app/views/templates/new.html.erb new file mode 100644 index 0000000..e92a243 --- /dev/null +++ b/app/views/templates/new.html.erb @@ -0,0 +1,20 @@ +
+
+
+
+
+
+

Nouveau template

+
+
+ <%= render 'form', template: @template %> + + +
+
+ <%= link_to 'Back', templates_path %> +
+ +
+
+
diff --git a/app/views/templates/show.html.erb b/app/views/templates/show.html.erb new file mode 100644 index 0000000..690c5fa --- /dev/null +++ b/app/views/templates/show.html.erb @@ -0,0 +1,31 @@ +
+ <%= link_to 'Retourner à la liste des séances', templates_path, class:"pl-3" %> + +
+
+
+
+
+

Séance

+
+ + <%= link_to 'Editer la séance', edit_template_path(@template) %> + +
+ +

+ Intitulé + <%= @template.title %> +

+ +

+ Description + <%= @template.description %> +

+ +
+
+
+
+
+
diff --git a/config/routes.rb b/config/routes.rb index 53f70c7..83e0ac0 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -11,6 +11,9 @@ resources :students do collection { post :import } end + resources :templates do + get 'duplicate' + end resources :student_worksessions, only: [:destroy] get 'report', to: 'home#report' get 'home/index', to: "home#index" From e79bbcded3bc726b3839a57e84ff509189114abe Mon Sep 17 00:00:00 2001 From: JonathanPreminger Date: Thu, 14 Mar 2019 10:43:46 +0100 Subject: [PATCH 2/2] add the begining of the drop down in the index template --- app/views/templates/index.html.erb | 3 +++ 1 file changed, 3 insertions(+) diff --git a/app/views/templates/index.html.erb b/app/views/templates/index.html.erb index 195a5c8..4d84f3f 100644 --- a/app/views/templates/index.html.erb +++ b/app/views/templates/index.html.erb @@ -21,6 +21,9 @@ + <% @templates.each do |f| %> + <%= f.collection_select :template, @templates.all, :title, :description, { class: 'chosen-select'} %> + <% end %>