Class: Conversations::ConversationsController

Inherits:
ApplicationController
  • Object
show all
Includes:
ApplicationHelper
Defined in:
app/controllers/conversations/conversations_controller.rb

Instance Method Summary collapse

Instance Method Details

#createObject



12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'app/controllers/conversations/conversations_controller.rb', line 12

def create
  @user = User.find(params[:user_id])
  @conversation = Conversation.new params[:conversations_conversation]
  @conversation.user_conversations.build
  @conversation.user_conversations.first.user = @user
  # @conversation.messages.build if @conversation.messages.empty?
  @conversation.messages = []
  @conversation.messages.build body: params[:conversations_conversation][:messages_attributes]['0'][:body], user_id: @user.id
  if @conversation.save
    redirect_to user_conversation_path(@user, @user.user_conversations.last), notice: 'Conversation was successfully created.'
  else
    render action: "new"
  end
end

#newObject



5
6
7
8
9
10
# File 'app/controllers/conversations/conversations_controller.rb', line 5

def new
  @user = User.find(params[:user_id])
  @conversation = Conversation.new
  @conversation.messages.build
  @conversation.users << User.where(id: params[:to_ids]) if params[:to_ids]
end