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:
If both commands give you version numbers, you're good to go!
Now let’s create your first app. In your terminal, run:
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:
Now go into your app folder:
And run the Rails server:
Or simply:
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 (likePost
,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:
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!
-
Generate a new controller:
This will create:
-
A
pages_controller.rb
file -
A view file at
app/views/pages/hello.html.erb
-
-
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