tabletop-companion/app/controllers/characters_controller.rb

63 lines
1.3 KiB
Ruby

# frozen_string_literal: true
class CharactersController < ApplicationController
before_action :set_character, only: [ :show, :edit, :update, :destroy ]
def index
@characters = Current.user.characters
end
def new
@character = Current.user.characters.new
end
def create
@character = Current.user.characters.new(character_params)
if @character.save
redirect_to @character, notice: t(".success")
else
flash.now[:alert] = t(".error")
render :new, status: :unprocessable_entity
end
end
def show
end
def edit
end
def update
if @character.update(character_params)
redirect_to @character, notice: t(".success", name: @character.name)
else
flash.now[:alert] = t(".error")
render :edit, status: :unprocessable_entity
end
end
def destroy
name = @character.name
if @character.destroy
redirect_to characters_path, notice: t(".success", name:)
else
flash[:alert] = t(".error")
redirect_to characters_path
end
end
private
def set_character
@character = Current.user.characters.find(params[:id])
end
def character_params
params.require(:character).permit(
:name,
:game_system_id,
:table_id,
)
end
end