diff options
Diffstat (limited to 'app')
-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 | ||||
-rw-r--r-- | app/views/characters/new.html.erb | 7 | ||||
-rw-r--r-- | app/views/characters/show.html.erb | 4 |
5 files changed, 49 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! 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> |