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!

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