67 lines
		
	
	
		
			1.4 KiB
		
	
	
	
		
			Ruby
		
	
	
	
	
	
			
		
		
	
	
			67 lines
		
	
	
		
			1.4 KiB
		
	
	
	
		
			Ruby
		
	
	
	
	
	
| class PuzzlesController < ApplicationController
 | |
|   before_action :set_contest
 | |
|   before_action :set_puzzle, only: %i[ destroy edit update]
 | |
| 
 | |
|   def edit
 | |
|     authorize @contest
 | |
| 
 | |
|     @action_name = t("helpers.buttons.back")
 | |
|     @action_path = contest_path(@contest)
 | |
|   end
 | |
| 
 | |
|   def new
 | |
|     authorize @contest
 | |
| 
 | |
|     @action_name = t("helpers.buttons.back")
 | |
|     @action_path = contest_path(@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), notice: t("puzzles.new.notice")
 | |
|     else
 | |
|       @action_name = t("helpers.buttons.back")
 | |
|       @action_path = contest_path(@contest)
 | |
|       render :new, status: :unprocessable_entity
 | |
|     end
 | |
|   end
 | |
| 
 | |
|   def update
 | |
|     authorize @contest
 | |
| 
 | |
|     if @puzzle.update(puzzle_params)
 | |
|       redirect_to @contest, notice: t("puzzles.edit.notice")
 | |
|     else
 | |
|       @action_name = t("helpers.buttons.back")
 | |
|       @action_path = contest_path(@contest)
 | |
|       render :edit, status: :unprocessable_entity
 | |
|     end
 | |
|   end
 | |
| 
 | |
|   def destroy
 | |
|     authorize @contest
 | |
| 
 | |
|     @puzzle.destroy
 | |
|     redirect_to contest_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
 |