From a7bda7b9a5dfcda926c7743d53bbf76cd159d661 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juan=20V=C3=A1squez?= Date: Wed, 8 Oct 2025 10:21:15 -0600 Subject: [PATCH 1/2] Add Puzzle validation --- app/assets/stylesheets/application.css | 13 ++++++ app/controllers/puzzles_controller.rb | 11 ++--- app/models/puzzle.rb | 2 + app/views/puzzles/_edit_modal.html.erb | 11 +++++ app/views/puzzles/_form.html.erb | 7 ++- app/views/puzzles/edit.html.erb | 15 +----- test/controllers/puzzles_controller_test.rb | 37 +++++++++++++++ test/fixtures/puzzles.yml | 17 +++++++ test/models/puzzle_test.rb | 52 +++++++++++++++++++++ 9 files changed, 143 insertions(+), 22 deletions(-) create mode 100644 app/views/puzzles/_edit_modal.html.erb create mode 100644 test/fixtures/puzzles.yml create mode 100644 test/models/puzzle_test.rb diff --git a/app/assets/stylesheets/application.css b/app/assets/stylesheets/application.css index 25a39b1..3913646 100644 --- a/app/assets/stylesheets/application.css +++ b/app/assets/stylesheets/application.css @@ -153,4 +153,17 @@ th { gap: 0.3em; } +.field-error { + color: #dc3545; + font-size: 12px; + margin-top: 4px; + font-weight: 500; +} + +input.error, textarea.error { + width: 100%; + border-color: #dc3545; + box-shadow: 0 0 0 0.2rem rgba(220, 53, 69, 0.25); +} + diff --git a/app/controllers/puzzles_controller.rb b/app/controllers/puzzles_controller.rb index 72380c5..8771524 100644 --- a/app/controllers/puzzles_controller.rb +++ b/app/controllers/puzzles_controller.rb @@ -16,15 +16,14 @@ def edit def update @puzzle = Puzzle.find(params[:id]) - if @puzzle.update(puzzle_params) - respond_to do |format| + + respond_to do |format| + if @puzzle.update(puzzle_params) format.turbo_stream format.html { redirect_to puzzles_path, notice: "Puzzle updated." } format.json { render json: { success: true, puzzle: @puzzle } } - end - else - respond_to do |format| - format.turbo_stream { render turbo_stream: turbo_stream.replace(@puzzle, partial: "puzzles/form", locals: { puzzle: @puzzle }) } + else + format.turbo_stream { render turbo_stream: turbo_stream.replace("modal", partial: "puzzles/edit_modal", locals: { puzzle: @puzzle }), status: :unprocessable_entity } format.html { render :edit, status: :unprocessable_entity } format.json { render json: { success: false, errors: @puzzle.errors.full_messages }, status: :unprocessable_entity } end diff --git a/app/models/puzzle.rb b/app/models/puzzle.rb index c7ee3f5..ebb5c64 100644 --- a/app/models/puzzle.rb +++ b/app/models/puzzle.rb @@ -3,5 +3,7 @@ class Puzzle < ApplicationRecord enum :state, { approved: 0, rejected: 1, pending: 2, archived: 3 } has_many :answers + validates :question, presence: true + scope :archived, -> { where(state: :archived).order(sent_at: :desc) } end diff --git a/app/views/puzzles/_edit_modal.html.erb b/app/views/puzzles/_edit_modal.html.erb new file mode 100644 index 0000000..6f33c7a --- /dev/null +++ b/app/views/puzzles/_edit_modal.html.erb @@ -0,0 +1,11 @@ + + + diff --git a/app/views/puzzles/_form.html.erb b/app/views/puzzles/_form.html.erb index a342f8c..8689dba 100644 --- a/app/views/puzzles/_form.html.erb +++ b/app/views/puzzles/_form.html.erb @@ -1,7 +1,10 @@ -<%=form_with model: @puzzle do |f| %> +<%= form_with model: puzzle do |f| %>