summaryrefslogtreecommitdiff
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
parentcd04bcc4b4e4f5174d2f042bfcf6b840e6e18fee (diff)
Login with forced character creation, views, etc
-rw-r--r--app/controllers/application_controller.rb10
-rw-r--r--app/controllers/characters_controller.rb28
-rw-r--r--app/controllers/home_controller.rb2
-rw-r--r--app/views/characters/new.html.erb7
-rw-r--r--app/views/characters/show.html.erb4
-rw-r--r--config/routes.rb4
6 files changed, 52 insertions, 3 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!
diff --git a/app/views/characters/new.html.erb b/app/views/characters/new.html.erb
new file mode 100644
index 0000000..451d731
--- /dev/null
+++ b/app/views/characters/new.html.erb
@@ -0,0 +1,7 @@
+<h1 class="text-xl">New Character</h1>
+
+<%= form_with model: @character do |f| %>
+ <%= f.label :name, "Name" %>
+ <%= f.text_field :name %>
+ <%= f.submit "Create" %>
+<% end %>
diff --git a/app/views/characters/show.html.erb b/app/views/characters/show.html.erb
new file mode 100644
index 0000000..8a3b08b
--- /dev/null
+++ b/app/views/characters/show.html.erb
@@ -0,0 +1,4 @@
+<h1 class="text-xl">
+ <%= @character.name %>
+</h1>
+<p>First entered the planes <%= pluralize((Date.current - @character.created_at.to_date).to_i, "day") %> ago.</p>
diff --git a/config/routes.rb b/config/routes.rb
index 6754031..e048a85 100644
--- a/config/routes.rb
+++ b/config/routes.rb
@@ -1,4 +1,6 @@
Rails.application.routes.draw do
+ root to: "home#index", as: :home_index
+
devise_scope :user do
get "login", to: "devise/sessions#new"
get "logout", to: "devise/sessions#destroy"
@@ -9,5 +11,5 @@ Rails.application.routes.draw do
put "users", to: "devise/registrations#update", as: "user_registration"
end
- root to: "home#index", as: :home_index
+ resources :characters, only: [:show, :new, :create]
end