Monday, April 21, 2025

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 what actually happens when someone visits a URL in your app.

In this post, we’ll explore:

  • 🚦 What controllers are and how to create them

  • 🧠 What actions are and how to work with parameters

  • 🖼️ How Rails renders views and uses layouts to keep things organized

Let’s jump right in!


Creating and Using Controllers 🚀

A controller in Rails is like a traffic controller—it decides what to do when a user visits a certain page (URL).

Each controller is a Ruby class that inherits from ApplicationController and has methods called actions.

Let’s create a controller

Open your terminal and run:

rails generate controller Pages home about contact

This does a few things:

  • Creates a file: app/controllers/pages_controller.rb

  • Adds three methods (actions): home, about, and contact

  • Creates matching view files:

    • app/views/pages/home.html.erb

    • app/views/pages/about.html.erb

    • app/views/pages/contact.html.erb

  • Updates your routes

Now open your browser and visit:

  • http://localhost:3000/pages/home

  • http://localhost:3000/pages/about

  • http://localhost:3000/pages/contact

🎉 You just created your first custom pages using a controller!


Action Methods and Parameters 🔍

Each method inside a controller is called an action. It runs when a user visits a matching route.

Here’s a simple example:

class PagesController < ApplicationController
def greet @name = params[:name] end end

Let’s also add a view for it: app/views/pages/greet.html.erb

<h1>Hello, <%= @name %>!</h1>

Now update your routes.rb:

get "/greet", to: "pages#greet"

Visit this URL in your browser:

http://localhost:3000/greet?name=John

Boom! You’ll see:
Hello, John!

👉 This is how parameters work in Rails. You can pass data through the URL using params[:key].


Rendering Views and Using Layouts 🖼️🧩

Views

A view is the HTML file Rails renders after a controller action runs. Views are stored in:

app/views/controller_name/action_name.html.erb

So for PagesController#greet, the view file should be:

app/views/pages/greet.html.erb

Inside .erb files, you can mix HTML with Ruby code using <%= %>.


Layouts

Rails uses layouts to keep your HTML structure consistent (like headers, footers, navbars).

Default layout:

app/views/layouts/application.html.erb

Inside this file, you’ll see:

<%= yield %>

This is where your actual view content gets inserted. So if your layout has a navbar and footer, they’ll always show—just like magic 🧙‍♂️

You can also use different layouts for different controllers if needed.


Wrapping Up 🎁

Nice job! Here’s what you’ve learned today:

  • ✅ How to create and use controllers

  • ✅ How action methods work and how to use params

  • ✅ How Rails renders views and keeps things consistent using layouts

In the next blog, we’re going to explore a super important part of any Rails app—working with data! You’ll learn:

  • 📦 What Active Record is and how Rails talks to the database

  • 🛠️ How to create models and migrations

  • ✏️ How to perform CRUD operations (Create, Read, Update, Delete)

We’ll finally make our app dynamic and start saving real data to the database. It’s going to be a big step forward—see you there! 🚀

As always, if you have any questions or want a concept explained more deeply—drop a comment or message. I’ve got you covered!

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!

Getting Started with Rails

 Hey there! If you're new to web development or just curious about Ruby on Rails, you’ve landed at the right place. In this blog, we're going to walk you through the basics of Rails. We’ll talk about what Rails is, how it works using the MVC pattern, and why it’s so developer-friendly thanks to something called Convention over Configuration.

Let’s dive in!


What is Ruby on Rails?

So, Ruby on Rails (or just Rails) is a web application framework. Think of it like a toolbox full of pre-built tools and parts that help you build websites and web apps quickly and easily. And it’s written in a programming language called Ruby, which is known for being clean and easy to read—almost like writing plain English.

Why do developers love Rails?

  • It saves time.

  • It lets you write less code to do more.

  • It’s beginner-friendly but powerful enough for big applications too.

Whether you're building a blog, an online store, or a social network, Rails has your back.


Understanding MVC in Rails (with an Example)

Rails uses something called the MVC architecture. Don’t worry—it sounds more complicated than it actually is. Let me break it down.

MVC stands for:

  1. Model – handles the data and rules of your app

  2. View – what the user sees (HTML pages)

  3. Controller – the middleman that connects models and views

Let’s take a simple example:

Imagine you're building a blog.

  • The Model is like the brain. It knows what a blog post is, stores the data (like title and content), and talks to the database.

  • The View is the face. It shows the blog posts on a web page that users can read.

  • The Controller is like the manager. It takes requests from users, tells the model what to do, and then chooses which view to show.

Example flow:

  • You go to www.myblog.com/posts

  • Rails goes to the PostsController and runs the index action

  • It asks the Post model to fetch all blog posts from the database

  • Then, it renders the index.html.erb view, showing the list of posts on the page

Pretty cool, right?


Convention Over Configuration (Let Rails Do the Heavy Lifting)

One of the best things about Rails is that it follows the idea of “Convention over Configuration.” What does that mean?

Basically, Rails assumes a lot of things for you so you don’t have to set up everything manually.

Example:

  • You create a model called Post

  • Rails automatically looks for a database table called posts

  • You make a controller called PostsController, and Rails expects the views to be in a folder called app/views/posts/
  • When a user submits a form to /posts, Rails knows it should go to the create action inside PostsController

So instead of spending time writing a lot of setup code, you can focus on building your features.

It’s like Rails is saying:
“If you follow my rules, I’ll make your life a lot easier.”



Final Thoughts

Getting started with Rails doesn’t have to be scary. In fact, it’s pretty fun once you get the hang of it. We covered:

  • What Rails is and why it’s awesome

  • How the MVC pattern keeps your code organized

  • How Rails conventions save you time and effort

This is just the beginning. In the next blog, we’ll start creating a simple Rails app step-by-step.

Thanks for reading! Got questions or want something explained in more detail? Drop a comment—I’d love to help!


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 ...