From 4c4b9b78e2a88b20680f3bf7b4cfcbc3da8de533 Mon Sep 17 00:00:00 2001 From: David Gay Date: Mon, 30 Oct 2023 02:26:25 -0400 Subject: Checkpoint creation --- app/controllers/checkpoints_controller.rb | 27 +++++++++++++++++++++ app/helpers/checkpoints_helper.rb | 2 ++ app/views/checkpoints/new.html.erb | 31 +++++++++++++++++++++++++ app/views/runs/show.html.erb | 11 +++++++++ config/routes.rb | 4 +++- test/controllers/checkpoints_controller_test.rb | 7 ++++++ 6 files changed, 81 insertions(+), 1 deletion(-) create mode 100644 app/controllers/checkpoints_controller.rb create mode 100644 app/helpers/checkpoints_helper.rb create mode 100644 app/views/checkpoints/new.html.erb create mode 100644 test/controllers/checkpoints_controller_test.rb diff --git a/app/controllers/checkpoints_controller.rb b/app/controllers/checkpoints_controller.rb new file mode 100644 index 0000000..94df2c3 --- /dev/null +++ b/app/controllers/checkpoints_controller.rb @@ -0,0 +1,27 @@ +class CheckpointsController < ApplicationController + before_action :set_run + + def new + @checkpoint = @run.checkpoints.build + end + + def create + @checkpoint = @run.checkpoints.build(checkpoint_params) + @checkpoint.user = current_user + if @checkpoint.save + redirect_to run_path(@run), notice: "Saved checkpoint." + else + render :new, status: :unprocessable_entity + end + end + +protected + + def checkpoint_params + params.require(:checkpoint).permit(:title, :description, :save_file) + end + + def set_run + @run = Run.find(params[:run_id]) + end +end diff --git a/app/helpers/checkpoints_helper.rb b/app/helpers/checkpoints_helper.rb new file mode 100644 index 0000000..421e4aa --- /dev/null +++ b/app/helpers/checkpoints_helper.rb @@ -0,0 +1,2 @@ +module CheckpointsHelper +end diff --git a/app/views/checkpoints/new.html.erb b/app/views/checkpoints/new.html.erb new file mode 100644 index 0000000..fc61e9f --- /dev/null +++ b/app/views/checkpoints/new.html.erb @@ -0,0 +1,31 @@ +<%= form_with model: [@run, @checkpoint] do |f| %> +
+ <%= f.label :title %> + <%= f.text_field :title %> +
+ +
+ <%= f.label :description %> + <%= f.text_area :description %> +
+ +
+ <%= f.label :save_file %> + <%= f.file_field :save_file %> +
+ +
+ <%= f.submit "Submit checkpoint" %> +
+ + <% if @checkpoint.errors.any? %> +
+

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

+ +
+ <% end %> +<% end %> diff --git a/app/views/runs/show.html.erb b/app/views/runs/show.html.erb index adb41e4..11fdd12 100644 --- a/app/views/runs/show.html.erb +++ b/app/views/runs/show.html.erb @@ -2,3 +2,14 @@

<%= @run.game.title %>

Run started by: <%= @run.user.email %>

+ +<%= link_to "New checkpoint", new_run_checkpoint_path(@run) %> + +

Checkpoints

+ + diff --git a/config/routes.rb b/config/routes.rb index a267aa8..c601180 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -9,5 +9,7 @@ Rails.application.routes.draw do # Defines the root path route ("/") root "home#index" - resources :runs + resources :runs do + resources :checkpoints + end end diff --git a/test/controllers/checkpoints_controller_test.rb b/test/controllers/checkpoints_controller_test.rb new file mode 100644 index 0000000..c4459d2 --- /dev/null +++ b/test/controllers/checkpoints_controller_test.rb @@ -0,0 +1,7 @@ +require "test_helper" + +class CheckpointsControllerTest < ActionDispatch::IntegrationTest + # test "the truth" do + # assert true + # end +end -- cgit v1.2.3