implement appending messages to conversations
parent
156632c5d7
commit
b14a2a603f
|
|
@ -1,5 +1,22 @@
|
||||||
# frozen_string_literal: true
|
# frozen_string_literal: true
|
||||||
|
|
||||||
class ConversationsController < ApplicationController
|
class ConversationsController < ApplicationController
|
||||||
def index; end
|
def index
|
||||||
|
@conversations = @current_user.conversations
|
||||||
|
end
|
||||||
|
|
||||||
|
def show
|
||||||
|
@conversation = @current_user.conversations.find(params[:id])
|
||||||
|
@messages = @conversation.messages
|
||||||
|
@prompt = Message.new(conversation: @conversation)
|
||||||
|
end
|
||||||
|
|
||||||
|
def create
|
||||||
|
@conversation = @current_user.conversations.new
|
||||||
|
if @conversation.save
|
||||||
|
redirect_to @conversation
|
||||||
|
else
|
||||||
|
render :index, status: :unprocessable_entity
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,19 @@
|
||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
class MessagesController < ApplicationController
|
||||||
|
def create
|
||||||
|
# TODO: Revisit auth once cancancan is installed
|
||||||
|
@message = Message.new(message_params)
|
||||||
|
if @message.save
|
||||||
|
redirect_to @message.conversation
|
||||||
|
else
|
||||||
|
redirect_to conversations_path
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
def message_params
|
||||||
|
params.require(:message).permit(:conversation_id, :body)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
@ -3,5 +3,5 @@
|
||||||
class Conversation < ApplicationRecord
|
class Conversation < ApplicationRecord
|
||||||
belongs_to :user
|
belongs_to :user
|
||||||
|
|
||||||
has_many :messages, dependent: :destro
|
has_many :messages, dependent: :destroy
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,11 @@
|
||||||
|
<div>
|
||||||
|
<%= form_with do |form| %>
|
||||||
|
<%= form.submit "Start a new conversation" %>
|
||||||
|
<% end %>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<li>
|
||||||
|
<% @conversations.each do |conversation| %>
|
||||||
|
<%= link_to conversation %>
|
||||||
|
<% end %>
|
||||||
|
</li>
|
||||||
|
|
@ -0,0 +1,15 @@
|
||||||
|
<div>
|
||||||
|
<%= link_to "list conversations", conversations_path %>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<ul>
|
||||||
|
<% @messages.each do |message| %>
|
||||||
|
<li><%= message.body %></li>
|
||||||
|
<% end %>
|
||||||
|
</ul>
|
||||||
|
|
||||||
|
<%= form_with model: @prompt do |form| %>
|
||||||
|
<%= form.text_area :body %>
|
||||||
|
<%= form.hidden_field :conversation_id %>
|
||||||
|
<%= form.submit "send message" %>
|
||||||
|
<% end %>
|
||||||
|
|
@ -7,5 +7,6 @@ Rails.application.routes.draw do
|
||||||
post :login, to: 'sessions#create'
|
post :login, to: 'sessions#create'
|
||||||
post :logout, to: 'sessions#destroy'
|
post :logout, to: 'sessions#destroy'
|
||||||
|
|
||||||
resources :conversations
|
resources :conversations, only: %i[index show create destroy]
|
||||||
|
resources :messages, only: %i[create]
|
||||||
end
|
end
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue