Class: HuntersController

Inherits:
ApplicationController show all
Defined in:
app/controllers/hunters_controller.rb

Overview

Restful Hunters Controller

Instance Method Summary collapse

Methods inherited from ApplicationController

#check_for_mobile, #mobile_device?

Instance Method Details

#createObject

POST /hunters POST /hunters.json Creates a new Hunter


43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'app/controllers/hunters_controller.rb', line 43

def create
  @hunter = Hunter.new(hunter_params)
  @hunter.user = current_user
  @hunter.assign_attributes(experience: 0, harm: 0, luck: 7)
  authorize @hunter

  respond_to do |format|
    if @hunter.save
      format.html { redirect_to hunter_path(@hunter), notice: 'Hunter was successfully created.' }
      format.json { render :show, status: :created, location: @hunter }
    else
      format.html { render :new }
      format.json { render json: @hunter.errors, status: :unprocessable_entity }
    end
  end
end

#destroyObject

DELETE /hunters/1 DELETE /hunters/1.json

Parameters:

  • id (Integer)

    Id from the Hunter table of the Hunter to be destroyed


82
83
84
85
86
87
88
# File 'app/controllers/hunters_controller.rb', line 82

def destroy
  @hunter.destroy
  respond_to do |format|
    format.html { redirect_to hunters_url, notice: 'Hunter was successfully destroyed.' }
    format.json { head :no_content }
  end
end

#editObject

GET /hunters/1/edit

Parameters:

  • id (Integer)

    id of the Hunter to be edited


36
# File 'app/controllers/hunters_controller.rb', line 36

def edit() end

#hunter_paramsActionController::Parameters

Private Method

Specify acceptable params for the Hunter object

Parameters:

  • hunter (Hash)

    contains the following params:

  • name (String)

    the Hunter's Name

  • playbook_id (Integer)

    References the id of the Playbook in the the Playbook table

  • harm (Integer)

    The amount of Harm the hunter has taken

  • luck (Integer)

    The amount of luck the hunter has left

  • experience (Integer)

    The amount of experience the hunter has not spent.

  • charm (Integer)

    the modifier for the Hunter's charm rating

  • cool (Integer)

    the modifier for the Hunter's cool rating

  • sharp (Integer)

    the modifier for the Hunter's sharp rating

  • tough (Integer)

    the modifier for the Hunter's tough rating

  • weird (Integer)

    the modifier for the Hunter's weird rating

  • rating_id (Integer)

    the id of the Rating object to attach

  • retired (boolean)

    whether or not the hunter has retired

Returns:

  • (ActionController::Parameters)

115
116
117
118
119
120
# File 'app/controllers/hunters_controller.rb', line 115

def hunter_params
  params.require(:hunter)
        .permit(:name, :playbook_id, :harm, :luck, :experience,
                :charm, :cool, :sharp, :tough, :weird, :rating_id,
                :retired, gear_ids: [], move_ids: [])
end

#indexObject

GET /hunters GET /hunters.json List all hunters


10
11
12
# File 'app/controllers/hunters_controller.rb', line 10

def index
  @hunters = policy_scope(Hunter)
end

#newObject

GET /hunters/new Routes to the new Hunter form. It initializes experience and harm to 0, and luck to 7.


28
29
30
31
# File 'app/controllers/hunters_controller.rb', line 28

def new
  @hunter = Hunter.new(experience: 0, harm: 0, luck: 7)
  authorize @hunter
end

#showObject

GET /hunters/1 GET /hunters/1.json

Parameters:

  • :id (Integer)

    An id in the Hunter table, selects the hunter to return

  • :available_improvements (Boolean)

    Toggles whether or not to include a list of improvements available to the hunter

See Also:


21
22
23
# File 'app/controllers/hunters_controller.rb', line 21

def show
  @available_improvements = params[:available_improvements]
end

#updateObject

PATCH/PUT /hunters/1 PATCH/PUT /hunters/1.json Update the Hunter


65
66
67
68
69
70
71
72
73
74
75
76
# File 'app/controllers/hunters_controller.rb', line 65

def update
  respond_to do |format|
    @hunter.user ||= current_user
    if @hunter.update(hunter_params)
      format.html { redirect_to hunter_path(@hunter), notice: 'Hunter was successfully updated.' }
      format.json { render :show, status: :ok, location: @hunter }
    else
      format.html { render :edit }
      format.json { render json: @hunter.errors, status: :unprocessable_entity }
    end
  end
end