/sweetify

SweetAlert flash messages for Ruby on Rails

Primary LanguageRubyBSD 3-Clause "New" or "Revised" LicenseBSD-3-Clause

Sweetify - SweetAlert for Rails

Gem Build Status

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

Installation

Add this line to your application's Gemfile:

gem 'sweetify'

And then execute:

$ bundle

Add this line to your application.scss

@import 'sweetalert';

Add this line to your application.js:

//=require sweetalert

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

...

<%= render 'sweetify/alert' %>

</body> </html>

You must 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 specified
sweetalert(text, title = '', opts = {})

# Additional methods with the type already defined
sweetalert_info(text, title = '', opts = {})
sweetalert_success(text, title = '', opts = {})
sweetalert_error(text, title = '', opts = {})
sweetalert_warning(text, title = '', opts = {})

Example Usage

# POST /resource
def create
    sweetalert_success('Your resource is created and available.', 'Successfully created', persistent: 'Awesome!')
    redirect_to resource_path
end

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 automatically
sweetalert('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 pressed
sweetalert('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: