Class Central is learner-supported. When you buy through links on our site, we may earn an affiliate commission.

Treehouse

Rails Routes and Resources Course (How To)

via Treehouse

Overview

You've seen how to create a Rails resource using a scaffold. But scaffolds don't offer flexibility in how a resource is set up. Let's create a resource totally by hand to see how it's put together. Along the way, we'll learn another important component of Rails: routes.

What you'll learn

  • Routes
  • Models
  • Views
  • Controllers

Syllabus

A Route to an Index Action

In the first stage, we'll read all Page records in the database and display their titles in a list. (Since we won't yet have a form for adding Pages, we'll have to add some via the Rails console.) This first stage will be the most involved, since we'll need to set up a route, a controller, a view, AND a model for our Pages.

Chevron 14 steps
  • Introduction

    2:03

  • Creating a Route

    4:45

  • Creating a Route

    4 questions

  • Viewing Routes

    1 objective

  • Adding a Route

    1 objective

  • Creating a Controller

    2:39

  • Creating a Controller

    1 objective

  • Creating a View

    1:29

  • Creating a View

    1 question

  • Creating a Model

    3:32

  • Creating a Model

    1 objective

  • Adding Records via Rails Console

    3:17

  • Populating the View

    8:57

  • Populating the View

    2 objectives

A Route to a Read Action

Users can view a list of all our page titles, but they can't view the content of single Pages yet. For our Posts resource, they can click a link on the list of all Posts to view an individual post. Let's set up links to view single Pages as well.

Chevron 9 steps
  • Linking to Pages

    3:56

  • Linking to Model Objects

    1 objective

  • Set Up a URL Parameter

    1 objective

  • Path Helpers

    1:38

  • Path Helpers

    1 objective

  • Finding a Page

    2:55

  • Finding a Model Object

    1 objective

  • Creating a Show View

    2:01

  • Creating a Show View

    1 objective

Routes to Create Actions

Our users can view a list of all Pages. They can click a link to view an individual Page. What they CAN'T do right now is create new pages; we had to go into the Rails console to do that. Our next task is to set up a form so that it's easy for our USERS to create Pages, too.

Chevron 10 steps
  • Route for New Pages

    3:08

  • Route for New Pets

    1 objective

  • Controller Action for New Pages

    1:48

  • View for New Pages

    5:42

  • Controller Action for New Pets

    1 objective

  • View for New Pets

    4 objectives

  • Route to Create Pages

    1:18

  • Route to Create Pets

    1 objective

  • Controller Action to Create Pages

    11:27

  • Controller Action to Create Pets

    5 objectives

Routes to Update Actions

Now our users can create a new Page. But they'd better get it right the first time, because right now they can't go back and change what they've entered.

Maybe we should fix that. Let's give them a form to allow them to update existing Pages.

Chevron 6 steps
  • An Edit Route

    6:24

  • An Edit Route

    1 objective

  • An Edit Form

    5:31

  • Moving the Pet Form to a Partial

    5 objectives

  • An Update Action

    5:52

  • An Update Action

    5 objectives

A Route to a Delete Action

For every resource you create, there may come a time when you don't want it any more. So, let's allow our users to delete Pages.

Chevron 6 steps
  • Deleting Pages

    5:30

  • A Delete Action

    3 objectives

  • Rails Resources

    3:50

  • A Pets Resource

    1 objective

  • Filters

    3:41

  • Summary

    0:45

Taught by

Jay McGavren

Reviews

Start your review of Rails Routes and Resources Course (How To)

Never Stop Learning.

Get personalized course recommendations, track subjects and courses with reminders, and more.

Someone learning on their laptop while sitting on the floor.