About the gem “toastr-rails”

Tram Ho

Are you fed up with displaying flash notifications like this?

And want to convert to display like this:

So gem toastr-rails is for you!

1. General introduction

toastr is a Javascript library developed based on Jquery and is built to serve the display of messages in a more beautiful and customizable way. In addition to the toastr-rails gem used in Rails, you can also find toast on the Bootstrap 4 library, but this gem was built to better support Rails.

The current version of gem toastr-rails is 2.0.1.

You can see the demo of this gem here: https://codeseven.github.io/toastr/demo.html

2. Usage example

1. Install Jquery on Rails

More

into Gemfile.

More

go to application.js. If you are using Rails 5.1+ then just add the line //= require jquery to this file.

2. Install gem toastr-rails

More

into Gemfile.

More

go to application.js.

And

go to application.css.

3. Create a helper

And add the following line to the layout, just below the js include tag

4. Use

After having the other helper, using toast on the controller is no different from using flash, we just need to use simple as follows:

If used in a JS file, gem toastr-rails already supports the following functions:

5. Customize

In addition to the display, we can change a few parameters for toastr. We can change these parameters by editing the object toastr.options in the js file (in the following example is the application.js file but you can also change it as required on your application)

3. Reference source

  1. https://github.com/tylergannon/toastr-rails
  2. https://coderwall.com/p/ximm8a/coverting-rails-flash-messages-to-toastr-notifications
Share the news now

Source : Viblo