From 3d7a033f0827a7bcfb1a46efcdd53676ed685f93 Mon Sep 17 00:00:00 2001 From: Sebskyo Date: Sun, 2 Mar 2025 08:51:43 +0100 Subject: [PATCH] ability to delete conversations --- app/controllers/conversations_controller.rb | 8 ++++++++ app/models/ability.rb | 4 ++-- app/views/conversations/show.html.erb | 4 ++++ 3 files changed, 14 insertions(+), 2 deletions(-) diff --git a/app/controllers/conversations_controller.rb b/app/controllers/conversations_controller.rb index d0d9ba8..592741a 100644 --- a/app/controllers/conversations_controller.rb +++ b/app/controllers/conversations_controller.rb @@ -21,4 +21,12 @@ class ConversationsController < ApplicationController render :index, status: :unprocessable_entity end end + + def destroy + if @conversation.destroy + redirect_to conversations_path + else + render :show, status: :unprocessable_entity + end + end end diff --git a/app/models/ability.rb b/app/models/ability.rb index b5c7243..919bd99 100644 --- a/app/models/ability.rb +++ b/app/models/ability.rb @@ -5,8 +5,8 @@ class Ability def initialize(user) can :create, Conversation - can :read, Conversation, user: user + can %i[read destroy], Conversation, user: user can :create, Message, conversation: { user: user } - can %i[index read], Message, conversation: { user: user } + can %i[index read destroy], Message, conversation: { user: user } end end diff --git a/app/views/conversations/show.html.erb b/app/views/conversations/show.html.erb index 08245b2..4453a64 100644 --- a/app/views/conversations/show.html.erb +++ b/app/views/conversations/show.html.erb @@ -2,6 +2,10 @@ <%= link_to "list conversations", conversations_path %> +<%= form_with url: conversation_path(@conversation), method: :delete do |form| %> + <%= form.submit "delete conversation" %> +<% end %> +