class PokedexEntriesController < ApplicationController before_action :set_run before_action :only_allow_if_checked_out, only: [:create] def index @pokedex_entries = PokedexEntry.where(run: @run).order(:created_at).reverse end def new @pokedex_entry = PokedexEntry.new end def create @pokedex_entry = current_user.pokedex_entries.build(pokedex_entry_params) @pokedex_entry.run_id = params[:run_id] if @pokedex_entry.save redirect_to run_pokedex_entries_path(@run) else render :new, status: :unprocessable_entity end end protected def pokedex_entry_params params.require(:pokedex_entry).permit(:pokemon_id, :recorded_at, :shiny) end def set_run @run = Run.find(params[:run_id]) end def only_allow_if_checked_out redirect_to run_path(@run) unless @run.checked_out_user == current_user end end