58 lines
1003 B
Ruby
58 lines
1003 B
Ruby
class PuzzlesController < ApplicationController
|
|
before_action :set_contest
|
|
before_action :set_puzzle, only: %i[ destroy edit update]
|
|
|
|
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_path(@contest)
|
|
else
|
|
render :new, status: :unprocessable_entity
|
|
end
|
|
end
|
|
|
|
def update
|
|
authorize @contest
|
|
|
|
if @puzzle.update(puzzle_params)
|
|
redirect_to @contest
|
|
else
|
|
render :edit, status: :unprocessable_entity
|
|
end
|
|
end
|
|
|
|
def destroy
|
|
authorize @contest
|
|
|
|
@puzzle.destroy
|
|
redirect_to contest_path(@contest)
|
|
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 ])
|
|
end
|
|
end
|