-2

I have a simple rails app where users can post and others user A can create a post and all other users can comment on that post and after that user A can send messages to the users who had commented on the post...

What am trying to do is to add direct messaging link that on which if an user clicks it will directly open the message box with the name of the user already added in the recipient.

I am using mailboxer gem for messaging functionality.

i had followed this tutorial

Ahmed Reza
  • 293
  • 1
  • 3
  • 19

1 Answers1

0

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.

Rob Hughes
  • 876
  • 2
  • 13
  • 32
  • it does not worked giving error: undefined method `id' for nil:NilClass – Ahmed Reza Feb 02 '16 at 05:33
  • Don't just copy @ user.id, you need to find the ID of your user that you want to send the message to. Eg. @ post.comment.user.id depending on how your relationships are set up and where you want the button. As you have not posted any code, I cannot help you any further. – Rob Hughes Feb 02 '16 at 11:42