Send message link:
<%= link_to '', new_conversation_path(:recipient_id => @user.id), class: 'send-message-icon' %>
@user.id is the ID of the user you want to send to.
_form:
<%= form_for :conversation, url: :conversations, html: { class: "" } do |f| %>
<div class="form-group">
<%= f.label :recipients %>
<%= hidden_field_tag(:recipient_id, "#{@user.id}") %></div>
<div class="form-group">
<%= f.label :subject %>
<%= f.text_field :subject, placeholder: "Subject", class: "form-control" %>
</div>
<div class="form-group">
<%= f.label :message %>
<%= f.text_area :body, class: 'form-control',placeholder: "Type your message here", rows: 4 %>
</div>
<%= f.submit "Send Message", class: "btn btn-success" %>
<% end %>
This gets rid of the select box and show the username of the recipient as set in the link.
Controller:
def new
@user = User.find_by(id: params[:recipient_id])
end
def create
recipients = User.find_by(id: params[:recipient_id])
conversation = current_user.send_message(recipients, conversation_params[:body], conversation_params[:subject]).conversation
flash[:success] = "Your message was successfully sent!"
respond_to do |format|
format.html {redirect_to conversation_path(conversation)}
format.js
end
end
Sends the message.