conversations_controller.rb 713 B

123456789101112131415161718192021222324252627282930
  1. class ConversationsController < ApplicationController
  2. before_action :set_conversation, only: [:show]
  3. before_action :check_permission, only: [:show]
  4. def index
  5. @conversations = current_user.conversations.sort_by{
  6. |c| c.messages.last.created_at
  7. }
  8. end
  9. def show
  10. end
  11. private
  12. def set_conversation
  13. @conversation = Conversation.find(params[:id])
  14. end
  15. def check_permission
  16. unless current_user.conversations.include? @conversation
  17. flash[:danger] = "You are not part of this conversation"
  18. redirect_to root_path
  19. end
  20. end
  21. def conversation_params
  22. params.require(:conversation).permit(:name, messages_attributes: [:content])
  23. end
  24. end