Make A Jobs Home Page

Goals

  • create a page that should display a list of jobs

  • Play follow-the-error to guide our development

  • Add routes, a controller, and a view to make a page

  • Learn how to read the routing info at http://localhost:3000/rails/info

Steps

Step 1: access /jobs

Let's add a page to list the open jobs at RailsBridgeCorp!

This list should have the following URL - let's see if its already there? http://localhost:3000/jobs.

Error! Woo!!!

No route matches [GET] '/jobs'

That's pretty reasonable, since we currently don't have any routes at all right now.

Step 2: add a route

So it's looking for a route, but can't find one. Let's add one!

Open up the routes file. It's in the config directory, called routes.rb. If you're using VS Code, you can open it using keyboard shortcuts:

  • Hitting cmd + p (on Mac) or ctl + p (on PC)
  • typing in route
  • hitting enter

Magic! (VS Code is using fuzzy search, so you can use the entire file path, or just part of the filename to go to files.)

We're going to need a resource route, which will create EIGHT different routes for us. Add this to line two:

resources :jobs

Now, lets go look at what that made, by using the excellently helpful routes page: http://localhost:3000/rails/info.

Discussion: How to read the routes page.


In your browser, visit http://localhost:3000/rails/info. Go over each of the columns and how to read it.

  • Helper
  • HTTP Verb
  • Path
  • Controller#Action

Can you find the line that will make /jobs a route?

Since adding the line resources :jobs made a route matching /jobs, let's go visit that page again: http://localhost:3000/jobs

Error! Woo!!!

uninitialized constant JobsController

Step 3: create a controller

Why does Rails now think it needs a JobsController?

Add a controller

Time for a shortcut!!! Unlike the scaffold that we make in Suggestotron that makes about a million different files, we're just going to use a Rails generator to make the files we need.

Type this in the shell:
rails generate controller jobs

Discussion: What did that command do?


What files were made by that last command?

How will this change what error we see when we go to the jobs page?

Error! Woo!!!

The action 'index' could not be found for JobsController

Step 4: set up an action

Let's go back to http://localhost:3000/rails/info again and look at what controller and method (AKA action) the route /jobs is pointing to.

It's looking for a method called index on the jobs controller! Since there isn't a controller method for this route, we'll need to add it.

Open up your jobs controller (again, try to use keyboard shortcuts: cmd+p on Mac or ctl+p on PC to find the file at app/controllers/jobs_controller.rb).

Add the index method to the controller:

class JobsController < ApplicationController
  def index
  end
end

Error! Woo!!!

JobsController#index is missing a template for request formats: text/html.

Step 5: create a view

What's a template? How does Rails decide to look for the template associated with JobsController's index action?

Talk through what Rails is trying, and failing, to do, and how file names and method names are important here.

Add a view

Within app/views/jobs, create a file called index.html.erb

add some html code to it

<h1>Jobs</h1>
<p>coming soon</p>

Now there should be no more errors

Congratulations, your are done!

If you want, you can add some more HTML content to your first view.

Now would be a good time to commit to git

Next Step: