Listing The Jobs

Goals

  • Show the jobs!

  • Learn about ERB

Going back to the jobs index (http://localhost:3000/jobs), we expect to see the list of all jobs. Let's actually build the job board part of this job board now!

Steps

Step 1: read the jobs in the console

Before we show the jobs, let's actually look at what that is doing. Go back to your Rails console and run Job.all.

Discussion: Rails Console


The Rails console is super fun! It's giving us direct access to our local database.

  • Try running Job.all.to_sql. What does that do?
  • Try selecting an individual Job record.
  • Try updating that individual record from the console!

Step 2: read the jobs in the controller

If we're going to show our jobs in view, first we have to get them out of the database and store them in an instance variable.

Update the index method in the JobsController to look like this

def index
  @jobs = Job.all
end

Step 3: show the jobs in the view

Add this to app/views/jobs/index.html.erb:

<% @jobs.each do |job| %>
  <h3><%= job.title %></h3>
  <p><%= job.description %></p>
<% end %>

Discussion: ERB


What is this doing? Go through this line by line, having one person explain each line.

Compare the ERB to the HTML that shows up on the page. ("Inspect Element" is your friend!)

What's the difference between a line with <% %> brackets and <%= %> brackets?

Now would be a good time to commit to git

Next Step: