diff --git a/app/controllers/contests_controller.rb b/app/controllers/contests_controller.rb index 72c28be..5b289eb 100644 --- a/app/controllers/contests_controller.rb +++ b/app/controllers/contests_controller.rb @@ -8,6 +8,7 @@ class ContestsController < ApplicationController def show @title = @contest.name + @puzzles = @contest.puzzles set_badges end diff --git a/app/controllers/puzzles_controller.rb b/app/controllers/puzzles_controller.rb index 83ad779..71462d1 100644 --- a/app/controllers/puzzles_controller.rb +++ b/app/controllers/puzzles_controller.rb @@ -1,4 +1,5 @@ class PuzzlesController < ApplicationController + before_action :set_contest before_action :set_puzzle, only: %i[ show destroy ] def index @@ -10,13 +11,16 @@ class PuzzlesController < ApplicationController def new @puzzle = Puzzle.new + @title = "New contest puzzle" end def create @puzzle = Puzzle.new(puzzle_params) + @puzzle.contest_id = @contest.id if @puzzle.save - redirect_to @puzzle + redirect_to contest_path(@contest) else + @title = "New contest puzzle" render :new, status: :unprocessable_entity end end @@ -28,6 +32,10 @@ class PuzzlesController < ApplicationController private + def set_contest + @contest = Contest.find(params[:contest_id]) + end + def set_puzzle @puzzle = Puzzle.find(params[:id]) end diff --git a/app/models/contest.rb b/app/models/contest.rb index 5ed21d6..060698f 100644 --- a/app/models/contest.rb +++ b/app/models/contest.rb @@ -1,3 +1,4 @@ class Contest < ApplicationRecord - belongs_to :user + belongs_to :user, dependent: :destroy + has_many :puzzles end diff --git a/app/models/puzzle.rb b/app/models/puzzle.rb index 2162e67..7630b01 100644 --- a/app/models/puzzle.rb +++ b/app/models/puzzle.rb @@ -1,4 +1,5 @@ class Puzzle < ApplicationRecord + belongs_to :contest, dependent: :destroy has_one_attached :image validates :name, presence: true end diff --git a/app/views/contests/show.html.slim b/app/views/contests/show.html.slim index d6cf88f..ef5f285 100644 --- a/app/views/contests/show.html.slim +++ b/app/views/contests/show.html.slim @@ -1,10 +1,31 @@ -.row.mb-4 +.row.mb-2 .col - @badges.each do |badge| span.badge.text-bg-info.me-2 = badge -.row +.row.mb-4 .col a.btn.btn-primary href=edit_contest_path(@contest) - | Edit \ No newline at end of file + | Edit contest + +.row.mb-4 + .col + h4 Puzzles + .float-end + a.btn.btn-primary href=new_contest_puzzle_path(@contest) + | Add puzzle + .row.row-cols-1.row-cols-md-3.g-4.mb-4 + - @puzzles.each do |puzzle| + .col + .card.h-100 + .card-header + = puzzle.name + = image_tag puzzle.image, class: "card-img-top" if puzzle.image.attached? + .card-body + p.card-text + | TODO puzzle.brand + a.btn.btn-primary href=edit_contest_puzzle_path(@contest, puzzle) + | Edit + .col + h4 Teams \ No newline at end of file diff --git a/app/views/puzzles/_form.html.slim b/app/views/puzzles/_form.html.slim new file mode 100644 index 0000000..3f783d1 --- /dev/null +++ b/app/views/puzzles/_form.html.slim @@ -0,0 +1,13 @@ += form_with model: puzzle, url: "/contests/#{contest.id}/puzzles", method: :post do |form| + .row.mb-3 + .col + .form-floating + = form.text_field :name, autocomplete: "off", class: "form-control" + = form.label :name, class: "required" + .row.mb-3 + .col + .form-text Select an image + = form.file_field :image, accept: "image/*", class: "form-control" + .row + .col + = form.submit submit_text, class: "btn btn-primary" \ No newline at end of file diff --git a/app/views/puzzles/new.html.slim b/app/views/puzzles/new.html.slim index c0d6254..fe1c544 100644 --- a/app/views/puzzles/new.html.slim +++ b/app/views/puzzles/new.html.slim @@ -1,11 +1 @@ -h1 New puzzle - -= form_with model: @puzzle do |form| - div - = form.label :name - = form.text_field :name - div - = form.label :image, style: "display: block" - = form.file_field :image, accept: "image/*" - div - = form.submit \ No newline at end of file += render "form", contest: @contest, puzzle: @puzzle, submit_text: "Add" \ No newline at end of file diff --git a/config/routes.rb b/config/routes.rb index 1347a77..397cf87 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -8,9 +8,10 @@ Rails.application.routes.draw do # Defines the root path route ("/") root "contests#index" - resources :contests + resources :contests do + resources :puzzles + end resources :passwords, param: :token - resources :puzzles resource :session resources :users end diff --git a/db/migrate/20250315124339_add_contest_ref_to_puzzle.rb b/db/migrate/20250315124339_add_contest_ref_to_puzzle.rb new file mode 100644 index 0000000..b9fc21b --- /dev/null +++ b/db/migrate/20250315124339_add_contest_ref_to_puzzle.rb @@ -0,0 +1,5 @@ +class AddContestRefToPuzzle < ActiveRecord::Migration[8.0] + def change + add_reference :puzzles, :contest, null: false, foreign_key: true + end +end diff --git a/db/schema.rb b/db/schema.rb index 2c79f33..bfcb963 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[8.0].define(version: 2025_03_15_114657) do +ActiveRecord::Schema[8.0].define(version: 2025_03_15_124339) do create_table "active_storage_attachments", force: :cascade do |t| t.string "name", null: false t.string "record_type", null: false @@ -53,6 +53,8 @@ ActiveRecord::Schema[8.0].define(version: 2025_03_15_114657) do t.string "name" t.datetime "created_at", null: false t.datetime "updated_at", null: false + t.integer "contest_id", null: false + t.index ["contest_id"], name: "index_puzzles_on_contest_id" end create_table "sessions", force: :cascade do |t| @@ -76,5 +78,6 @@ ActiveRecord::Schema[8.0].define(version: 2025_03_15_114657) do add_foreign_key "active_storage_attachments", "active_storage_blobs", column: "blob_id" add_foreign_key "active_storage_variant_records", "active_storage_blobs", column: "blob_id" add_foreign_key "contests", "users" + add_foreign_key "puzzles", "contests" add_foreign_key "sessions", "users" end