tabletop-companion/app/controllers/tables_controller.rb

68 lines
1.4 KiB
Ruby

# frozen_string_literal: true
class TablesController < ApplicationController
before_action :set_table, only: [ :show, :edit, :update, :destroy ]
before_action :set_characters, only: [ :show ]
def index
@tables = Current.user.tables
end
def show
@table_invites = @table.table_invites.not_responded
render layout: "table"
end
def new
@table = Table.new
end
def create
@table = Current.user.owned_tables.new(table_params)
if @table.save
redirect_to @table, notice: t(".success", name: @table.name)
else
flash.now[:alert] = t(".error")
render :new, status: :unprocessable_entity
end
end
def edit
end
def update
if @table.update(table_params)
redirect_to @table, notice: t(".success", name: @table.name)
else
flash.now[:alert] = t(".error")
render :edit, status: :unprocessable_entity
end
end
def destroy
if @table.destroy
redirect_to tables_path, notice: t(".success", name: @table.name)
else
flash[:alert] = t(".error", name: @table.name)
redirect_to tables_path
end
end
private
def set_table
@table = Current.user.tables.find(params[:id])
end
def set_characters
@characters = Current.user.characters.where(table: @table)
end
def table_params
params.require(:table).permit(
:name,
:game_system_id,
)
end
end