• Skip to main content
  • Skip to primary sidebar

Founder at work

Master the skills to create and launch your next SaaS company

  • About Me
  • Reviews
  • Resources
  • Community
  • Contact
  • Lessons
  • Series
  • Search
  • Login

Ruby on Rails 4 Flash messages with Bootstrap 3

October 17, 2014 by Rick Hernandez Leave a Comment

Rimas Screenshot of login prompt

Alright so you are using bootstrap 3 and ROR 4, now it’s time to start adding some alert messages to your application. If it’s a simple warning class from bootstrap that needs to be added to a view, why should we create a class then pass a name to a function that then goes through a useless unless statement? Don’t you think this is an overkill? I personally do! If you have a specific purpose for a piece of code that will be used in a specific manner why not just make the value static in your application? By the way many other languages encourage this like C#, not only does it make your code more readable but easier to maintain and code along. In this example I created a module with a type.rb file that is used to hold my static messages.

lib/ALERT/type.rb

module ALERT
    INFO = "alert alert-info"
    SUCCESS = "alert alert-success"
    DANGER = "alert alert-danger"
    WARNING = "alert alert-warning"
end

Now you must include this new module in your application or else the controller won’t be able to use it. Here is how to include modules files in ROR,
controllers/user_controller.rb

require "ALERT/type"

Now all throughout your controller all you have to do is call out the static value, in short this is just recreating ENUMS like your would find in other C family languages

Example:

flash[ALERT::WARNING] = "Invalid username/password combination."
Rick Hernandez

I always had a passion for the field of STEM (Science, Technology, Engineering, and Math) and I knew I wanted to do something to make a difference in the world. I just didn’t know where to start. I was an immigrant in a new country, grew up in a tough environment, and wasn’t sure how… Read More

Share this:

  • Click to share on Twitter (Opens in new window)
  • Click to share on Facebook (Opens in new window)

Related

Filed Under: Programming, Ruby, Ruby on Rails (ROR), Web Development

Primary Sidebar

Learn How To Take Your Idea And Convert It Into An App From Scratch Even If You Have Never Written A Single Line Of Code.

  • How to Find Your Next Great App Idea
  • The Secret To Getting PAID To Build Your Own App
  • How To Create Your App (SaaS, Mobile, VR, AR, Game)
  • How To Monetize Your Apps
Learn More
4.82 Ratings

Copyright © 2025 · JSecademy Blog Theme on Genesis Framework · WordPress · Log in