DEV Community

Cover image for Getting Started with Ruby on Rails - A Beginner's Guide
Wilbur Suero
Wilbur Suero

Posted on

Getting Started with Ruby on Rails - A Beginner's Guide

Ruby on Rails, commonly known as Rails, is a popular open-source web framework written in the Ruby programming language. It's known for its simplicity, elegance, and conventions that allow developers to build web applications quickly and efficiently. In this beginner's guide, we'll cover the basics of Ruby on Rails and what you need to know to get started.

What is Ruby on Rails?

Ruby on Rails is a full-stack web framework that provides everything you need to build a web application from scratch. It includes a variety of tools and libraries for handling the front-end, back-end, and database operations. Rails is known for its convention-over-configuration philosophy, which means that it follows established best practices and conventions to make development faster and easier.

Why Ruby on Rails?

There are several reasons why Ruby on Rails has become so popular among web developers. Some of the key benefits of using Rails include:

  • It's easy to learn: Rails has a simple, straightforward syntax that makes it easy for beginners to get started.
  • It's fast to develop: Rails includes a lot of built-in functionality, so you can build a web application quickly without having to write a lot of code.
  • It's flexible: Rails provides a lot of options for customization, so you can build web applications that meet your specific needs.
  • It's well-supported: Rails has a large and active community of developers, so you can find a lot of resources and support for your projects.

Getting Started with Rails

To get started with Ruby on Rails, you'll need to have a few things set up on your computer:

Once you have these things set up, you're ready to start building your first Rails application. To do this, you'll use the Rails command line interface (CLI) to generate the basic structure of your application. Here are the steps to generate a new Rails application:

  1. Open a terminal window and navigate to the directory where you want to create your application.
  2. Run the following command to generate a new Rails application: rails new my_app. Replace "my_app" with the name of your application.
  3. Navigate into the newly created directory using the cd command: cd my_app.
  4. Run the following command to start the Rails development server: rails server.
  5. Open your web browser and navigate to http://localhost:3000 to see your Rails application in action.

Building Your First Rails Application

Now that you have a basic Rails application up and running, you can start building your first web pages.

Rails uses a Model-View-Controller (MVC) architecture, which separates the data (model), presentation (view), and logic (controller) of your application.

In this beginner's guide, we'll cover the basics of building a simple Rails application that displays a list of articles.

Creating the model

In Rails, a model represents the data in your application. To create a model for your articles, you'll run the following command: rails generate model Article title:string body:text. This will generate a new model class called Article with two fields: title and body.

Creating the controller

A controller is responsible for handling user interactions and rendering the view. To create a controller for your articles, you'll run the following command: rails generate controller Articles. This will generate a new controller class called ArticlesController.

Creating the view

A view is responsible for rendering the HTML that is displayed in the browser. To create a view for your articles, you'll create a new file in the app/views/articles directory called index.html.erb. This file will contain the HTML code that is displayed when you view the list of articles.

Here is an example of what the view code (index.html.erb) for displaying the list of articles could look like:

<h1>List of Articles</h1>  
<table>   
  <thead>
    <tr>
      <th>Title</th>
      <th>Body</th>
    </tr>
  </thead>
  <tbody>
    <% @articles.each do |article| %>
      <tr>
        <td><%= article.title %></td>
        <td><%= article.body %></td>
      </tr>
    <% end %>
  </tbody> 
</table>
Enter fullscreen mode Exit fullscreen mode

This code uses ERB (Embedded Ruby) to dynamically render the data stored in the @articles instance variable. The <table> tag displays the data in a tabular format, with each article's title and body being displayed in its own row. The <% %> syntax is used to embed Ruby code in the HTML, and the <%= %> syntax is used to display the result of the Ruby code.

Adding the routes

In Rails, routes define the URLs that are used in your application. To add routes for your articles, you'll open the config/routes.rb file and add the following line: resources :articles. This will create the routes needed to view, create, update, and delete articles.

Adding sample data

To display a list of articles in your application, you'll need to add some sample data to your database. To do this, you'll run the following command to open the Rails console: rails console. Then, you'll use the following code to create a new article:

Article.create(title: "First Article", body: "This is my first article.") 
Article.create(title: "Second Article", body: "This is my second article.")
Enter fullscreen mode Exit fullscreen mode

Viewing the articles

To view the list of articles, you'll navigate to http://localhost:3000/articles in your web browser. You should see a list of the two articles you just created.

Conclusion In this beginner's guide, we've covered the basics of Ruby on Rails and what you need to know to get started. While there's still much more to learn, these steps should give you a good foundation to start building your first web application. Remember to check out the Rails documentation (https://guides.rubyonrails.org/) for more information and tutorials.

Happy coding!

Top comments (0)