Hidden Gems: paul_revere

Mike Burns

There are times when you must proclaim from the rooftops, for all to hear, that your Web site has something to say! “Hear ye! Hear ye! The Swedish are coming!”, just like Paul Reveresson said.

Thus we have the paul_revere gem.

Sample one-off announcement using the paul_revere gem

Install it

To start, toss the gem in your Gemfile:

gem 'paul_revere'

There is an included generator, so be sure to run it:

rails g paul_revere

This will generate a migration to add an announcements table, so run that:

rake db:migrate

Users of older versions of Rails will see a generated JavaScript file too.

We’re almost there. Next up modify your layout to have a place for announcements. In app/views/layouts/application.html.erb:

<%= render 'announcements/announcement_for_all' %>

And finally add the JavaScript to the asset pipeline. In app/assets/javascripts/application.js:

//= require announcements

Whew! A simple process that will only become easier with improvements to Rails.

Use it

% rails c
> Announcement.create!(body: "I'm as mad as hell and that's OK!")

That was easy

As you just saw, using it is straightforward. This builds on an old RailsCast dealing with this topic. With these pieces you can build more complex tools into your app: an admin section that can make announcements, a dashboard where users can see prior announcements, a record of how many announcements you make per month, and so on.

Don’t re-invent the wheel; use the paul_revere gem today!