-
Notifications
You must be signed in to change notification settings - Fork 17
/
messages_controller.rb
52 lines (41 loc) · 1009 Bytes
/
messages_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
class MessagesController < ApplicationController
before_action :set_room, only: %i[ create ]
before_action :set_message, only: %i[ update edit destroy show ]
before_action :authorize_message, except: %i[ create ]
def create
@message = @room.messages.new(message_params)
@message.author = current_user
if @message.save
render turbo_stream: turbo_stream.append(:messages, @message)
else
render 'new', layout: false, status: :unprocessable_entity
end
end
def update
if @message.update(message_params)
render @message
else
render 'edit', layout: false, status: :unprocessable_entity
end
end
def edit
end
def destroy
@message.destroy
end
def show
end
private
def set_room
@room = Room.find(params[:room_id])
end
def set_message
@message = Message.find(params[:id])
end
def authorize_message
authorize @message
end
def message_params
params.require(:message).permit(:content)
end
end