65 lines
1.2 KiB
Ruby
65 lines
1.2 KiB
Ruby
class PuzzlesController < ApplicationController
|
|
before_action :set_contest
|
|
before_action :set_puzzle, only: %i[ destroy edit update]
|
|
|
|
def index
|
|
authorize @contest
|
|
|
|
@title = @contest.name
|
|
@puzzles = @contest.puzzles.order(:id)
|
|
end
|
|
|
|
def edit
|
|
authorize @contest
|
|
end
|
|
|
|
def new
|
|
authorize @contest
|
|
|
|
@puzzle = Puzzle.new
|
|
end
|
|
|
|
def create
|
|
authorize @contest
|
|
|
|
@puzzle = Puzzle.new(puzzle_params)
|
|
@puzzle.contest_id = @contest.id
|
|
if @puzzle.save
|
|
redirect_to contest_puzzles_path(@contest), notice: t("puzzles.new.notice")
|
|
else
|
|
render :new, status: :unprocessable_entity
|
|
end
|
|
end
|
|
|
|
def update
|
|
authorize @contest
|
|
|
|
if @puzzle.update(puzzle_params)
|
|
redirect_to contest_puzzles_path(@contest), notice: t("puzzles.edit.notice")
|
|
else
|
|
render :edit, status: :unprocessable_entity
|
|
end
|
|
end
|
|
|
|
def destroy
|
|
authorize @contest
|
|
|
|
@puzzle.destroy
|
|
redirect_to contest_puzzles_path(@contest), notice: t("puzzles.destroy.notice")
|
|
end
|
|
|
|
private
|
|
|
|
def set_contest
|
|
@contest = Contest.find(params[:contest_id])
|
|
end
|
|
|
|
def set_puzzle
|
|
@puzzle = Puzzle.find(params[:id])
|
|
end
|
|
|
|
def puzzle_params
|
|
params.expect(puzzle: [ :brand, :name, :image, :pieces ])
|
|
end
|
|
end
|