Delete Job Listings

Goals

  • Remove a job - Let's add a way to delete postings.

Postings be gone!

Once a job is filled, we don't want a listing for it hanging out forever.

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

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

Steps

Step 1: Send DELETE

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.

Add this line below the edit posting link in index.html.erb

<h5><%= button_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!

Step 2: add the destroy 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!

Now would be a good time to commit to git

Next Step: