Monday, April 21, 2025

Building Your First Rails App

 Hey there! đź‘‹ In the last blog, we talked about what Ruby on Rails is, how it works using the MVC architecture, and how Rails makes life easier with conventions. Now it’s time to get our hands dirty and build something!

In this post, we’re going to:

  • Set up a brand-new Rails project

  • Walk through the folder structure (so you know where everything lives)

  • Get introduced to the Rails router (routes.rb) and how it handles URLs

Ready? Let’s go!


Setting Up a New Rails Project

Before we start building, make sure you have Rails and Ruby installed. If you’re not sure, open your terminal and run:

rails -v
ruby -v

If both commands give you version numbers, you're good to go!

Now let’s create your first app. In your terminal, run:

rails new my_first_app

This command creates a new folder called my_first_app with all the files you need to start building a web app.

Quick Tip:

If you want to use a specific database (like PostgreSQL), you can do:

rails new my_first_app -d postgresql

Now go into your app folder:

cd my_first_app

And run the Rails server:

rails server

Or simply:

rails s

Now open your browser and go to http://localhost:3000 – you should see the “Yay! You’re on Rails!” page. 🎉


Understanding Rails Folder Structure

Let’s take a quick tour of the files and folders Rails created for you.

Here are some important ones:

app/ – This is where the magic happens!

  • models/ – Your data and logic go here (like Post, User, etc.)

  • views/ – HTML templates that users see

  • controllers/ – The traffic cops of your app, handling requests and talking to models/views

config/ – App settings live here

  • routes.rb – Defines the URLs of your app and connects them to controllers

db/ – Stuff related to your database

  • schema.rb – Keeps track of your database structure

Gemfile – List of Ruby gems (libraries) your app uses

You don’t need to memorize everything now. As we build stuff, you’ll naturally get familiar with these.


Introduction to the Rails Router (routes.rb)

Let’s talk about how Rails knows where to go when someone visits a URL.

Open config/routes.rb – this is the file that connects browser requests to the right controller action.

Here’s a super simple example:

Rails.application.routes.draw do
get "/hello", to: "pages#hello" end

What does this mean?

  • When someone goes to http://localhost:3000/hello

  • Rails will go to the PagesController

  • And run the hello method (called an "action")

Let’s try it!

  1. Generate a new controller:

    rails generate controller Pages hello

    This will create:

    • A pages_controller.rb file

    • A view file at app/views/pages/hello.html.erb

  2. Now visit http://localhost:3000/hello and boom – your first custom route works!


Wrapping Up

Congrats 🎉 You just:

  • Created your first Rails project

  • Learned what each folder does

  • Wrote your first custom route and saw it work in the browser

You’re off to a great start!

In the next blog, we’ll take a closer look at the heart of a Rails app—Controllers, Views, and Actions. You’ll learn:

  • How to create and use controllers

  • How action methods work (and how to use parameters)

  • How Rails renders views and uses layouts to keep your pages clean and consistent

It’s going to be super helpful as we start building actual features into our app. See you there!

No comments:

Post a Comment

Controllers, Views, and Actions in Rails 🚦🖼️

 Hey again! đź‘‹ In the last blog, we created our first Rails app and explored the folder structure and routing system. Now, it’s time to see ...