Search code examples
ruby-on-railsselectbuttonmailboxer

Ruby on rails. Mailboxer send message button


I'm having trouble making a send message button work with mailboxer gem. I used this tutorial as a guide 'http://josephndungu.com/tutorials/private-inbox-system-in-rails-with-mailboxer' where as many others, you must choose the recipient from a list of all users.

I would like a button to send a message directly with no lists so I have made a 'send user a message' button on a page, passing the user id to the new conversation form.

The message doesn't get sent, and only shows up in the sender's sent box.

here is my code:

<%= link_to '', new_conversation_path(:recipients_id => @user.id), class: 'send-message-icon' %>

Also tried:

<%= link_to '', new_conversation_path(:recipients_id => @user), class: 'send-message-icon' %>

_form: (Correct user ID shows in the text box. I will later remove the text box)

<%= form_for :conversation, url: :conversations, html: { class: "" } do |f| %>
<div class="form-group">
  <%= f.label :recipients %>
  <%= f.text_field :recipients, :value => params[:recipients_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" %>

controller:

def create
    recipients = User.where(id: conversation_params[:recipients])
    conversation = current_user.send_message(recipients, conversation_params[:body], conversation_params[:subject]).conversation
    flash[:success] = "Your message was successfully sent!"
    redirect_to conversation_path(conversation)
  end

User model:

acts_as_messageable
  before_destroy { messages.destroy_all }

I can't understand why it isn't working. Any suggestions would be appreciated, thanks.


Solution

  • After hours of #$%"#"$%#$@ing around I got it working.

    Send message link from wherever:

    <%= link_to '', new_conversation_path(:recipient_id => @user.id), class: 'send-message-icon' %>
    

    _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 %>
    

    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!"
        redirect_to conversation_path(conversation)
      end