Rails
Ruby on Rails, commonly called Rails, is an open-source web application framework developed using the Ruby programming language.
Rails provides a collection of tools, libraries, and predefined structures that help developers create web applications quickly and easily.
Ruby is a programming language, whereas Rails is a framework that uses Ruby to develop web applications.
Rails was created by David Heinemeier Hansson (DHH) and released in 2004.
Example applications: E-commerce websites, blogs, student management systems, online booking systems, and content management systems.
Features of Rails
The following are the important features of Rails:
- Open Source: Rails is freely available, and developers can use and modify its source code.
- MVC Architecture: Rails follows the Model–View–Controller architecture to organize application code.
- Convention over Configuration: Rails provides standard naming rules and directory structures, reducing the need for manual configuration.
- DRY Principle: DRY stands for Don't Repeat Yourself. It encourages programmers to avoid writing the same code repeatedly.
- Database Support: Rails supports databases such as SQLite, MySQL, and PostgreSQL.
- Code Reusability: Developers can reuse existing code and components.
- Rapid Development: Rails provides built-in tools that make application development faster.
- Security: Rails provides built-in mechanisms to help protect applications against common web security threats.
MVC Architecture in Rails
MVC stands for Model–View–Controller. It is a software architecture used to divide an application into three main components. Those are
- Model
- View
- Controller
Fig: Model-View-Controller Architecture
1. Model
The Model is responsible for performing data validation and implementing business logic in the application. It manages application data, handles database operations and communicates with the database to store, retrieve, update and delete information.
2. View
The View represents the user interface of the application and is responsible for displaying content to the user. It generally contains HTML code along with embedded Ruby code to dynamically generate web pages.
3. Controller
The Controller is responsible for handling requests received from users and processing them accordingly. It communicates with the Model to retrieve, update or manage application data and selects the appropriate View to display the results to the user.
Working of MVC Architecture
Example: Consider a student checking their details on a website.
- The user sends a request through the browser.
- The Controller receives the request.
- The Controller communicates with the Model.
- The Model retrieves student information from the database.
- The Controller sends the information to the View.
- The View displays the student details in the browser.