-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanimals_controller.rb
More file actions
71 lines (61 loc) · 1.5 KB
/
animals_controller.rb
File metadata and controls
71 lines (61 loc) · 1.5 KB
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
class AnimalsController < ApplicationController
before_action :set_animal, only: [:show, :edit, :update, :destroy, :sort]
before_action :authenticate_user!
def index
@animals = Animal.all.order(:name)
@cats = Animal.where(species: 'cat').order(:name)
@dogs = Animal.where(species: 'dog').order(:name)
end
def new
@animal = Animal.new
end
def create
begin
@animal = Animal.new(animal_params)
@animal.species = @animal.species.downcase
@animal.save!
rescue ActiveRecord::RecordInvalid
set_flash
# Neat!
redirect_to new_animal_path
else
redirect_to @animal
end
end
def show
@doses = @animal.doses
@doses = @doses.order("#{params[:sort_by]} #{params[:order_by]}").joins(:drug, :user)
# Bonus points for joins!
end
def edit
end
def update
begin
@animal.update!(animal_params)
rescue ActiveRecord::RecordInvalid
set_flash
redirect_to edit_animal_path(@animal)
else
redirect_to @animal
end
end
def destroy
@animal.destroy
redirect_to animals_path
end
private
def animal_params
params.require(:animal).permit(:name, :weight, :species)
end
def set_animal
@animal = Animal.find(params[:id])
end
def set_flash
error_message = @animal.errors.full_messages.first
if error_message.downcase.include? "species"
flash[:alert] = "Please enter either cat or dog for the species"
else
flash[:alert] = error_message
end
end
end