diff options
Diffstat (limited to 'app/controllers')
-rw-r--r-- | app/controllers/application_controller.rb | 10 | ||||
-rw-r--r-- | app/controllers/characters_controller.rb | 28 | ||||
-rw-r--r-- | app/controllers/home_controller.rb | 2 |
3 files changed, 38 insertions, 2 deletions
diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 6b4dcfa..efd8427 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -1,3 +1,13 @@ class ApplicationController < ActionController::Base before_action :authenticate_user! + before_action :redirect_if_no_active_character, unless: :devise_controller? + + helper_method :current_char + def current_char + current_user&.active_character + end + + def redirect_if_no_active_character + redirect_to new_character_path unless current_char + end end diff --git a/app/controllers/characters_controller.rb b/app/controllers/characters_controller.rb new file mode 100644 index 0000000..d851ad5 --- /dev/null +++ b/app/controllers/characters_controller.rb @@ -0,0 +1,28 @@ +class CharactersController < ApplicationController + skip_before_action :redirect_if_no_active_character, only: [:new, :create] + + def show + @character = Character.find(params[:id]) + end + + def new + @character = Character.new + end + + def create + @character = Character.new(character_params) + @character.user = current_user + if @character.save + current_user.update(active_character: @character) + redirect_to @character + else + flash[:alert] = "Error creating character: #{@character.errors.full_messages.join(", ")}" + redirect_to action: :new + end + end + + private + def character_params + params.require(:character).permit(:name) + end +end diff --git a/app/controllers/home_controller.rb b/app/controllers/home_controller.rb index 57f762e..be6be01 100644 --- a/app/controllers/home_controller.rb +++ b/app/controllers/home_controller.rb @@ -1,5 +1,3 @@ -# frozen_string_literal: true - class HomeController < ApplicationController skip_before_action :authenticate_user! |