Add puzzles to contests
This commit is contained in:
@@ -8,6 +8,7 @@ class ContestsController < ApplicationController
|
||||
|
||||
def show
|
||||
@title = @contest.name
|
||||
@puzzles = @contest.puzzles
|
||||
set_badges
|
||||
end
|
||||
|
||||
|
@@ -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
|
||||
|
@@ -1,3 +1,4 @@
|
||||
class Contest < ApplicationRecord
|
||||
belongs_to :user
|
||||
belongs_to :user, dependent: :destroy
|
||||
has_many :puzzles
|
||||
end
|
||||
|
@@ -1,4 +1,5 @@
|
||||
class Puzzle < ApplicationRecord
|
||||
belongs_to :contest, dependent: :destroy
|
||||
has_one_attached :image
|
||||
validates :name, presence: true
|
||||
end
|
||||
|
@@ -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
|
||||
| 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
|
13
app/views/puzzles/_form.html.slim
Normal file
13
app/views/puzzles/_form.html.slim
Normal file
@@ -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"
|
@@ -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
|
||||
= render "form", contest: @contest, puzzle: @puzzle, submit_text: "Add"
|
Reference in New Issue
Block a user