Skip to content

Repository files navigation

Sweetify - SweetAlert for Rails

Gem

This gem allows you to use SweetAlert or SweetAlert2 for your flash messages. See the examples below, to see how to use it

Installation

Note: This package does not provide the client-side files of SweetAlert. You have to provide them yourself.

Add this line to your application's Gemfile:

gem'sweetify'

And then execute:

$ bundle

Next up, create the file sweetify.rb in your initializers to specify the library you are using (SweetAlert or SweetAlert2):

# possible options: 'sweetalert', 'sweetalert2' - default is 'sweetalert2'Sweetify.sweetalert_library='sweetalert2'

Next add the following line to the bottom of your application's layout file:

...
<%= render 'sweetify/alert' %></body></html>

You have to restart your rails server after installing the gem

Usage

You can now easily create alerts in your controllers with any of the following methods provided by Sweetify:

# Base Method, no type specifiedsweetalert(text,title='',opts={})# Additional methods with the type already definedsweetalert_info(text,title='',opts={})sweetalert_success(text,title='',opts={})sweetalert_error(text,title='',opts={})sweetalert_warning(text,title='',opts={})

Example Usage

# POST /resourcedefcreatesweetalert_success('Your resource is created and available.','Successfully created',persistent: 'Awesome!')redirect_toresource_pathend

That would look like this after the redirect:

Example Alert

Options

By default, all alerts will dismiss after a sensible default number of seconds.

Default Options set by Sweetify:

{showConfirmButton: false,timer: 2000,allowOutsideClick: true,confirmButtonText: 'OK'}

The following special options provided by Sweetify are available:

# Shows the alert with a button, but will still close automaticallysweetalert('Text','Title',button: true)sweetalert('Text','Title',button: 'Awesome!')# Custom text for the button# Shows the alert with a button and only closes if the button is pressedsweetalert('Text','Title',persistent: true)sweetalert('Text','Title',persistent: 'Awesome!')# Custom text for the button

You also can use any other available option that SweetAlert accepts:

sweetalert_info('Here is a custom image','Sweet!',imageUrl: 'images/thumbs-up.jpg',timer: 5000)

Contributing

Everyone is encouraged to help improve this project. Here are a few ways you can help:

About

SweetAlert flash messages for Ruby on Rails

Topics

Resources

Stars

20 stars

Watchers

4 watching

Forks

Releases

Packages

Used by

Contributors

Languages