For this post I want to talk about the basic structure of Ruby on Rails. Rails is a MVC software architectural pattern. Where M stands for model, V for view, and C for controller. Most websites communicate with databases to pull information and display it to the user. However, there are some that do not and those are called static pages. Rails though is a Ruby web application framework built to work with databases.
In order to control the information that is accessible from the database rails uses the MVC pattern. When a user types in a web address it first goes to your routes. Your routes will direct you to a controller. What is the controller’s job? Well it’s in the name. It controls a lot of the data flow of the application. It is the controller that will determine if the application will need to go to the database through the model to grab information or if it can just go directly and grab views to be displayed. If it wasn't for the controller it would be easy for a hacker to grab whatever information they want from the database. Some information may be sensitive. For example credit card information. Also the controller helps to keep hackers from dumping anything they want into your database. It is important that when you code out your controllers you use best practices for security measures.
If the page that is being accessed is just a static page it will go straight to the views. Otherwise the controller tells the model what it wants from the database. The model then fetches it from the database and sends it back to the controller. The Rails framework is set to allow the views to be able to pull the information it needs from the corresponding method in the controller. I hope to discuss the controller methods in more detail in another post.
Just in case you are still confused, I want to give an example. Let’s say you visit a blog that is built using Ruby on Rails. Like this one! When you select the blog you want to see, the routes will go to a method in a controller. Typically a controller set up specifically and solely for the blogs. The naming convention used for a method the displays one blog, is show. The show method in the blog controller will tell the model to go to the database and send back the specifically requested blog. The model will grab the blog from the database and send it to the controller. The controller then will send the blog to a show view. The end user then can see the blog and joyfully read. Or read with distaste.
Either way leave a comment and let me know what you think. Feel free to tell me if you think I am wrong with anything. Or ask questions.