summaryrefslogtreecommitdiff
path: root/app/controllers
diff options
context:
space:
mode:
authorDavid Gay <david@davidgay.org>2021-05-02 20:56:19 -0400
committerDavid Gay <david@davidgay.org>2021-05-02 20:56:19 -0400
commit1d5083ddf7f2b285e63bb8019d38199e862fa1d8 (patch)
tree6be0357a5e55f78faa94afbe9db025e0703f2f3b /app/controllers
parentcd04bcc4b4e4f5174d2f042bfcf6b840e6e18fee (diff)
Login with forced character creation, views, etc
Diffstat (limited to 'app/controllers')
-rw-r--r--app/controllers/application_controller.rb10
-rw-r--r--app/controllers/characters_controller.rb28
-rw-r--r--app/controllers/home_controller.rb2
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!