Messaging with Rails and Mailboxer

Okay, got it. I’ve added a piece of code by mistake. conversations_controller.rb, line 2

before_action :get_mailbox, except: [:destroy, :restore]

should be

before_action :get_mailbox

because otherwise there is no mailbox and therefore nowhere to find conversation. Going to fix this.

Great Article. The code your provided works great when using it with two user models. I did run into one small problem. When using mailboxer between two user models, the user id’s will conflict (ie. two user may have an id of 5). Is there any way around that?

That’s interesting. Unfortunately, I haven’t tried to implement such setup. Do you have your code somewhere so that I can a look?

I’m getting errors when I try to start new conversations. Can you please help me figure out exactly why this is happening?

SyntaxError app/views/messages/new.html.erb:5: syntax error, unexpected keyword_class, expecting keyword_do or ‘{’ or ‘(’
‘.freeze;@output_buffer.safe_append=’

syntax error, unexpected tSTRING_BEG, expecting keyword_end
…=( label_tag ‘message[subject]’, ‘Subject’ );@output_buffer

Started GET “/messages/new” for 127.0.0.1 at 2015-01-21 15:52:18 -0500
Processing by MessagesController#new as HTML
User Load (0.0ms) SELECT “users”.* FROM “users” WHERE “users”.“id” = ? ORDER BY “users”.“id” ASC LIMIT 1 [[“id”, 1]]
User Load (0.0ms) SELECT “users”.* FROM “users”
BizUser Load (1.0ms) SELECT “biz_users”.* FROM “biz_users”
Rendered messages/new.html.erb within layouts/application (10.0ms)
Completed 500 Internal Server Error in 50ms

ActionView::Template::Error (undefined method firstuser@hotmail.com_attacher' for nil:NilClass): 13: 14: <div class="form-group"> 15: <%= label_tag 'recipients', 'Choose recipients' %> 16: <%= select_tag 'recipients', recipients_options, multiple: true, class: 'form-control chosen-it' %> 17: </div> 18: 19: <%= submit_tag 'Send', class: 'btn btn-primary' %> app/helpers/messages_helper.rb:6:in block in recipients_options’

I’m getting these errors inside of the messages\new.html.erb form

<h1>Start Conversation</h1>

<%= form_tag messages_path, method: :post do %>
    <div class="form-group">
      <%= label_tag 'message[subject]', 'Subject' %>
      <%= text_field_tag 'message[subject]', nil, class: 'form-control', required: true %>
    </div>

    <div class="form-group">
      <%= label_tag 'message[body]', 'Message' %>
      <%= text_area_tag 'message[body]', nil, cols: 3, class: 'form-control', required: true %>
    </div>

    <div class="form-group">
      <%= label_tag 'recipients', 'Choose recipients' %>
      <%= select_tag 'recipients', recipients_options, multiple: true, class: 'form-control chosen-it' %>
    </div>

    <%= submit_tag 'Send', class: 'btn btn-primary' %>
<% end %>


def recipients_options
    s = ''
    users = User.all + BizUser.all; users.each do |user|
      s << "<option value=' #{user.id}' data-img-src='#{attachment_url(@user, user.email, :profile_avatar, :fill, 30, 30)}'> #{user.username}</option>"
    end
    s.html_safe
  end

I’m using refile gem btw for image attachments.

I can’t find error in this piece of code but that may be somewhere else. Do you have your project somewhere on GitHub or is it private?

I followed the same pattern with refile gem to add functionality for user avatars. But, that approach doesn’t work. Instead, I stayed with your method, and I will use refile for other elements in the application.

I wonder if there’s a cleaner way to resolve the issues with adding both user models to the mailboxer system. I’m currently getting a long reload time.

UPDATE: It started loading faster. I don’t know, maybe it was preparing the db. But, still, this approach of loading all users into the pull down list will become a problem when the user base grows?

Omg Ilya Bodrov you fulfilled my request. Thank you sooooo much.
Just saw the post. I am gonna try it out now.

It started loading faster. I don't know, maybe it was preparing the db. But, still, this approach of loading all users into the pull down list will become a problem when the user base grows? - yes, of course. If you have lots of users that form with dropdown is going to take much time to load.

You can either use model caching or implement a bit more advanced solution with AJAX to lazy load only the records that match the entered sample. Also it is a nice idea to send AJAX request when at least two (or three maybe) letters are entered in the field. You may also employ a jQuery plugin that fires an event when user stopped typing (or create such plugin yourself as it shouldn’t be hard).

Well, I liked your idea and the topic really appeared to be interesting :slight_smile:

Awesome guide. I tried a few others and this is by far the best. Thanks

Thank you!

This was fantastic! Super helpful and rapid way to get mailboxer up and running with components for customization. Much appreciated!

Oh, thanks a lot!

Firstable I must say that it was difficult for me to find a way to leave a comment.

in views/messages/new.html.erb

<% page_header "Send Message to #{@recipient.name}" %> # @recipient here is nil

I wonder how comes that I’m the only one encountering this issue.

Thank you for the great article, you might want to add some of this stuff to their docs on github. I think they’ll appreciate the help.

I’m really sorry, this piece of code was not updated in the tutorial. There should be no @recipient of course: https://github.com/bodrovis/SitePoint-Mailboxer/blob/master/app/views/messages/new.html.erb#L1 I will update this asap. Well noted!

P.S. https://github.com/mailboxer/mailboxer/issues/332 :slight_smile:

It would be useful if the UI of the chat was similar to that of facebook’s. Can this be done? The options of trash can all be arranged in the pop up box.

Also the current version can be maintained so it can be used in mobiles(as pop up like facebook in a mobile won’t be responsive and clutters the minimal space.)

Well, of course this all can be done, though that would require some time. :slight_smile:

Awesome tutorial!!! I’ve been trying to integrate this with my app where after a buyer makes a purchase from a seller, they both get routed to a messenger just been them.

How can this be done?

That’s an interesting question however I do not think Mailboxer provides such functionality out of the box. Probably what you could do is store buyer’s and seller’s ids and denote that only those two can participate in some conversation, so that no one else may add messages there.