summaryrefslogtreecommitdiff
path: root/app/controllers/characters_controller.rb
blob: 1a919881e3c8753d23992b05ec7382eef53bf5b9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
class CharactersController < ApplicationController
  skip_before_action :redirect_if_no_active_character, only: [:new, :create]
  before_action :set_character, only: [:show, :set_combat_styles]

  def show
  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

  def set_combat_styles
    if @character.update(offensive_style: params[:offensive_style],
                         defensive_style: params[:defensive_style])
      flash[:notice] = "Changed combat styles to #{@character.offensive_style} and #{@character.defensive_style}."
    else
      flash[:alert] = "Failed to set combat styles."
    end
    redirect_to character_path(@character)
  end

  private
    def character_params
      params.require(:character).permit(:name)
    end

    def set_character
      @character = Character.find(params[:character_id])
      unless current_char == @character
        flash[:alert] = "You can only manage your own character."
        redirect_to character_path(@character)
      end
    end
end