DEV Community

dgvall
dgvall

Posted on

Ruby on Rails - The Strengths of Convention Over Configuration

Ruby on Rails is a super popular web development framework that's all about Convention over Configuration. In other words, the framework follows a set of conventions so that developers don't have to specify every little detail themselves. This approach makes development way easier, reduces errors, and speeds up the whole process. Let's take a look at the benefits of Convention over Configuration in Ruby on Rails and how it helps developers out.

CLI Commands for Generators

One of the biggest perks of Ruby on Rails' Convention over Configuration is the use of Command Line Interface (CLI) commands for generators. These commands automatically create different parts of the application, like controllers, models, views, and migrations.

For example, you can use the "rails new" command to create a new application, which sets up a default file structure with directories for models, views, and controllers. This means you can dive right into the application without worrying about the initial setup.

Similarly, you can use the "rails g" command to generate different components of the application. You can use "rails g controller" to make a new controller, "rails g model" to create a new model, and "rails g migration" to create a new database migration.

These CLI commands save you so much time and effort, and make it less likely you'll make mistakes. I can't stress this point enough, it really helps you focus on the important parts of the application and let the framework take care of the boring parts.

File Structure and Separation of Concerns

Another benefit of Ruby on Rails' Convention over Configuration is the standard file structure and separation of concerns between the Model, View, and Controller (MVC) components. The MVC pattern divides the application into three different parts that handle different parts of the application.

The Model component deals with the data and business logic of the application. It stores the data and provides a way to interact with it. The View component handles the user interface and shows the data to the user. The Controller component connects the Model and View components and manages the flow of information between them.

Ruby on Rails follows a set of conventions for the file structure of these components, which makes it easier to understand and navigate the codebase. For instance, all models go in the "/app/models" directory, all views go in the "/app/views" directory, and all controllers go in the "/app/controllers" directory.

This separation of concerns means that each component is responsible for one specific part of the application, which reduces the likelihood of human error and makes it easier to read and maintain the code.

Customization and Flexibility

Although Ruby on Rails follows a set of conventions, developers can still customize and modify the framework to fit their needs. You can override the default behavior by specifying your own configuration files and adding custom code.

For example, you can change the default routes by adding your own routes in the "/config/routes.rb" file. Below are my routes on a project I recently completed. The "resources" code at the top is a conventional way to route common requests to a specified controller and controller action with a conventional path. Below that is customized code that I configured to route uncommon paths while still specifying a corresponding controller and controller action.

resources :tags, only: [:index]
  resources :artworks, only: [:create, :index, :update, :destroy]
  resources :user_likes, only: [:create, :destroy]

  post "/signup", to: "users#create"
  get "/me", to: "users#profile"
  get '/users/:username', to: 'users#show'

  get '/tags/:name', to: 'tags#filtered'

  post "/login", to: "sessions#create"
  delete "/logout", to: "sessions#destroy"
Enter fullscreen mode Exit fullscreen mode

Similarly, you can also create your own configuration files in the "config" directory, like "/config/application.rb," and"/config/database.yml" to customize the default behaviors of Rails.

This customization and flexibility mean that you can tailor the framework to your specific needs while still following the conventions. You can create the application you want without being restricted by the framework.

Conclusion

Overall, Convention over Configuration is a powerful concept that makes Ruby on Rails a great web development framework. By following a set of conventions, Ruby on Rails simplifies development, reduces errors, and makes it easier to maintain the application.

Top comments (0)