about summary refs log tree commit diff
path: root/app/controllers/machines_controller.rb
blob: 33dcd3b51724389ebd838da4af4ebdbbce3bca6c (plain) (blame)
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# frozen_string_literal: true

class MachinesController < ApplicationController
  before_action :set_machine, only: %i[show edit update destroy]

  # GET /machines or /machines.json
  def index
    @machines = Machine.order(:name)
  end

  # GET /machines/1 or /machines/1.json
  def show; end

  # GET /machines/new
  def new
    @machine = Machine.new
  end

  # GET /machines/1/edit
  def edit; end

  # POST /machines or /machines.json
  def create
    @machine = Machine.new(machine_params)

    respond_to do |format|
      if @machine.save
        format.html { redirect_to machines_url, notice: "Added #{@machine.name} #{@machine.edition}." }
        format.json { render :show, status: :created, location: @machine }
      else
        format.html { render :new, status: :unprocessable_entity }
        format.json { render json: @machine.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /machines/1 or /machines/1.json
  def update
    respond_to do |format|
      if @machine.update(machine_params)
        format.html { redirect_to machines_url, notice: "Updated #{@machine.name} #{@machine.edition}." }
        format.json { render :show, status: :ok, location: @machine }
      else
        format.html { render :show, status: :unprocessable_entity }
        format.json { render json: @machine.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /machines/1 or /machines/1.json
  def destroy
    @machine.destroy

    respond_to do |format|
      format.html { redirect_to machines_url, notice: 'Machine was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

  private

  # Use callbacks to share common setup or constraints between actions.
  def set_machine
    @machine = Machine.find(params[:id])
  end

  # Only allow a list of trusted parameters through.
  def machine_params
    params.require(:machine).permit(:name, :edition)
  end
end