I want to implement functionality where User A can send a message to User B. Say User A is viewing User B's profile and clicks on the Send Message link, how can I make sure that only User B will receive the message. In other words in the Create action how can I capture User B? I am able to capture User B in the new action but not in the create action. Here is what I have so far:
class MessagesController < ApplicationController
def new
@message = Message.new
@recipient = User.find_by_identifier!(params[:id])
end
def create
@message = Message.new(params[:message])
@message.recipient = @recipient
@message.sender = current_user
if @message.save
flash[:notice] = "Message has been sent"
redirect_to messages_path(:mailbox=>:sent)
else
render :action => :new
end
end
end
So the Send Message link passes in the params of User B for the new action. So @recipient
in the new action is User B and I am able to display User B's name and picture. But @recipient
is nil in the create action. How can I ensure that @recipient in the create action is User B?