Skip to content

Rails flash integration with PNotify

Ahmad Musaffa edited this page Sep 1, 2014 · 3 revisions

Setup PNotify gem:

Follow the instructions to setup pnotify-rails gem.

Or you can use rails-assets to use Pnotify's git repository as the source. To do this:

  1. add source 'https://rails-assets.org' below rubygem source declaration in the Gemfile.
  2. add gem 'rails-assets-pnotify'
  3. run bundle install
  4. add //= require pnotify in aplication.js
  5. add //= require pnotify in application.css

Setup Unobtrusive_flash gem:

  1. install unobtrusive_flash gem.
  2. add //= require unobtrusive_flash in application.js

Controllers:

  1. add after_filter :prepare_unobtrusive_flash to the top of the controllers that contain flashes. Or you can add this to the application controller as a global controller callback.
  2. define flashes in the controller actions as usual.

Views:

Remove any code in the views related to the flashes.

Javascript:

Add a flashes.js in the javascript assets directory:

// flashes.js
$(document).ready(function() {
  $(window).bind('rails:flash', function(e, params) {
    new PNotify({
      title: params.type,
      text: params.message,
      type: params.type
    });
  });
});

Now require flashes.js in the application.js. Remember to put it below require pnotify and require unobtrusive_flash.

Restart the server. Now You can Play!

Extra:

If you need I18n in javascript, checkout i18n-js.