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."
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