messages_controller.rb 660 B

123456789101112131415161718192021222324
  1. class MessagesController < ApplicationController
  2. def create
  3. @conversation = Conversation.find(params[:conversation_id])
  4. if current_user.conversations.include? @conversation
  5. @message = current_user.messages.build(message_params)
  6. @message.conversation = @conversation
  7. if @message.save
  8. flash[:success] = "Message posted successfully"
  9. redirect_to @conversation
  10. else
  11. render @conversation
  12. end
  13. else
  14. flash[:danger] = "You are not part of this conversation"
  15. redirect_to root_path
  16. end
  17. end
  18. private
  19. def message_params
  20. params.require(:message).permit(:content)
  21. end
  22. end