conversation_participations_controller.rb 878 B

12345678910111213141516171819202122232425262728293031
  1. class ConversationParticipationsController < ApplicationController
  2. before_action :set_conversation
  3. before_action :check_permissions, only: :create
  4. def create
  5. @participation = ConversationParticipation.create(participation_params)
  6. if @participation.save
  7. flash[:success] = "Added user to conversation"
  8. end
  9. redirect_to @conversation
  10. end
  11. private
  12. def participation_params
  13. params.require(:conversation_participation).permit(:conversation_id,
  14. :user_id)
  15. end
  16. def set_conversation
  17. @conversation = Conversation.find(participation_params[:conversation_id])
  18. end
  19. def check_permissions
  20. unless current_user.conversations.include? @conversation
  21. flash[:danger] = "You are not part of this conversation"
  22. redirect_to root_path
  23. end
  24. end
  25. end