in Ruby on Rails

Adding Private Messages in Rails Apps

I keep adding features to The latest and probably largest feature yet has been private messages. I looked around and there are quite a few gems out there that will do this for you, but they haven’t seemed to be updated in a while. After trying one out (and failing to get it to work properly) I decided it would be a good challenge for me to write my own.

Now, this private messaging app does not have all the bells and whistles that we are accustomed to today, but it’s simple and gets the job done. If time allows I’ll add more features to it but for now it’s very basic.

DotaWithMe has players and teams. I wanted a way for players to send a team a message (or each other) so they could make new friends. The first step for doing this was creating a new scaffold for messages. This ended up looking like:

This looked like this in my schema.rb:

The next step was in my /models/message.rb file I put: belongs_to :player and in models/player.rb I put: has_many :messages.

From there I started fiddling around with the best way to go about this. I’m going to post my messages_controller.rb first and then we can walk through my views.

The big things in my controller here are that: 1) We don’t want anyone to see a message unless it’s for them or from them (def index & def show) and 2) that you can’t send a message to no one (def new).

Now, onto the views! For the views I have 2 tables; one for messages received and one for messages sent. It looks vaguely like this:

As you can see I use an if statement to make sure the current user only sees their messages. link_to message.recipient and message.from takes you to the player’s profile page. I had a heck of a time trying to call player like this because I passed their username’s in instead of using identification numbers. Luckily I found out about this awesome .find_by ability and used that to find the ID based on the username. truncate is another nice little function that will limit a string/text to length x characters and add … when it reaches the maximum size. This is nice if you don’t want to display entire blocks of text on the main page.

My _form view is very simple and looks something like this:

I use hidden forms for the to and from so that the user doesn’t need to bother remembering who they were suppose to send a message to.

My views/messages/show.html.erb looks like:

Now the last part here involving the if statement simply adds a reply button if they are not the author. This is definitely not the greatest solution to messages, but it is a relatively easy and functional solution. The url structure means every message gets its own id in sequential order (ie .com/messages/1, /messages/2, etc etc). However we prevent others from viewing messages that don’t belong to them using our controller.

As I said earlier this is very rudimentary. There are no alerts for when you’ve received a new message and no way to mark messages as read. The other downside is that when one person deletes a message it is deleted for both parties. However, for my purposes currently it works. Again, I always welcome feedback and I hope this is useful to someone!

Write a Comment