Quantcast
Viewing latest article 10
Browse Latest Browse All 48

How to add a flash message to your Rails Twitter Bootstrap application

To get these nice Twitter Bootstrap alerts to show up in your Rails application

Image may be NSFW.
Clik here to view.
screen-shot-2016-12-04-at-4-08-53-pm

Emit them from your Rails app like this.

SomeController

 class SomeController < ApplicationController

  before_action :last_page

  def what_is_automated_testing
    flash[:success] = 'Success'
    flash[:info] = 'Info'
    flash[:warning] = 'Warning'
    flash[:danger] = 'Danger'
    render layout: "static_page_with_sidebar"
  end

Now, if you have Twitter bootstrap correctly configured, you should see this.

Image may be NSFW.
Clik here to view.
screen-shot-2016-12-04-at-4-13-29-pm

If you are wondering why you don’t see this styling with the rest of your application, it’s because Rails by default doesn’t use this hashes (success, info, warning, etc) for it’s messages. It uses ‘notice’ and others instead (though some may overlap) which is defined in the defaul scaffold.scss so beware of that.

Happy styling!

Links that help

https://agilewarrior.wordpress.com/2014/04/26/how-to-add-a-flash-message-to-your-rails-page/
http://getbootstrap.com/components/#alerts

 


Filed under: rails Tagged: twitter bootstrap Image may be NSFW.
Clik here to view.
Image may be NSFW.
Clik here to view.

Viewing latest article 10
Browse Latest Browse All 48

Trending Articles