-
Notifications
You must be signed in to change notification settings - Fork 344
/
contacts_controller.rb
72 lines (56 loc) · 1.63 KB
/
contacts_controller.rb
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
72
class Admin
class ContactsController < ApplicationController
before_action :authenticate_admin!
layout 'admin'
def edit
@location = Location.find(params[:location_id])
@contact = Contact.find(params[:id])
authorize @location
end
def update
@contact = Contact.find(params[:id])
@location = Location.find(params[:location_id])
authorize @location
if @contact.update(contact_params)
flash[:notice] = 'Contact was successfully updated.'
redirect_to [:admin, @location, @contact]
else
render :edit
end
end
def new
@location = Location.find(params[:location_id])
authorize @location
@contact = Contact.new
end
def create
@location = Location.find(params[:location_id])
@contact = @location.contacts.new(contact_params)
authorize @location
if @contact.save
flash[:notice] = "Contact '#{@contact.name}' was successfully created."
redirect_to admin_location_path(@location)
else
render :new
end
end
def destroy
contact = Contact.find(params[:id])
location = contact.location
authorize location
contact.destroy
redirect_to admin_location_path(location),
notice: "Contact '#{contact.name}' was successfully deleted."
end
private
def contact_params
params.require(:contact).permit(
:department, :email, :name, :title,
phones_attributes: %i[
country_prefix department extension number number_type
vanity_number id _destroy
]
)
end
end
end