Delete Job Listings

What we're going to do

  • Remove postings

Once a job is filled, we don't want a listing for it hanging out forever. Let's add a way to delete postings.

Postings be gone!

If we look at our handy Routes page, we see this:

job_path   DELETE   /jobs/:id(.:format)   jobs#destroy

So we need to send the DELETE http verb to the server, so we can get to the destroy method on the jobs controller (that we will make soon). It turns out rails link_to helpers accept specific verbs as an argument, so we can add this line below the edit posting link that we just added:

<h5><%= link_to 'Delete Posting', job, method: :delete %></h5>

Go to the index, and try to delete something.

Error! Woo!!!

The action 'destroy' could not be found for JobsController

This is like my favorite error now! First, let's add the method:

def destroy
end

This fixes the error. But just like updating, we still need to add the logic that actually deletes the job.

See if you can figure out the right syntax for finding the job, deleting it, and then redirecting to a useful page.

Don't scroll down!!!

  • here
  • is
  • even
  • more
  • strategic
  • white
  • space
  • so
  • the
  • answer
  • isn't
  • immediately
  • visible!

Okay, here's the answer:

@job = Job.find(params[:id])
@job.destroy
redirect_to jobs_path

Try it again, and... kablammo! We're destroying job listings left and right!

Next Step:

Back to Listing The Jobs