Make The Form Work

What we're going to do

  • Make the form actually work
  • Learn how to read server output
  • Review happy debugging techniques

Partytime: Use the form to Create Objects

Now, try to submit your form.

Error! Woo!!!

The action 'create' could not be found for JobsController

So, let's add it:

(Don't forget to navigate to the jobs controller by using the search shortcut, cmd+p or ctl+p)

def create
end

Try to use your form again. Unfortunately, there won't be any output, not even an error page. But if you look closely at the output from your Rails server, you might find this:

Error! Woo!!!

No template found for JobsController#create, rendering head :no_content

def create
  redirect_to jobs_path
end

Okay, now go to http://localhost:3000/jobs/new again, and submit a new job.

Hopefully it went to the right place! But did it do anything?

Sadly, no. We just took our form contents and dropped them on the ground.

Discussion: Logging & Server Output


Arrange your screens so that you can see your server logs (in the terminal window) at the same time as your browser. Now, refresh the form page, and look at what happens in the server. You should see output like Started GET "/jobs/new".

As a group or in pairs, go over the output of the server, talking through the various pieces of information you see.

Saving form data!

Head back to http://localhost:3000/jobs/new, and get your Rails server logs and your browser next to each other again. Submit the form again, this time looking for the section that looks something like:

Parameters: {
  "utf8"=>"✓",
  "authenticity_token"=>"f48rtxanK9/MHu7TPvd6QzygGnrwv0P2/bxLllozw5U=",
  "job"=>{
    "title"=>"Meta-organizer",
    "description"=>"We need an somene to organize the organizers."
  },
  "commit"=>"Create Job"
}

This is the precious data that our form is sending, and right now we're just throwing it away. Let's not do that! Since we're using Rails 5 and all its great conventions, we're going to use Strong Parameters to limit what kind of data our form can submit to our app.

Add this code to your jobs controller. (Notice that we're expanding the create method. Don't just copy and paste and end up with two create methods, folks.)

def create
  Job.create(job_params)
  redirect_to jobs_path
end

private

def job_params
  params.require(:job).permit(:title, :description)
end

Walk through this code line by line with the class! (But don't get too hung up on how strong params works — suffice it to say that it limits the parameters that we'll allow in to the ones listed here.)

Rails console time! Open up another tab or window in your terminal and type this:
rails c
After that's loaded, let's see how many jobs we've saved to the database:
Job.count

Now, submit a new job using the form. What's the count now?

If the count is going up, yay!!! You got it! If not, time to debug!

Tips for Effective Debugging

  • Take a deep breath. Most of your time programming will be looking for bugs.
  • Read the error message out loud. Even if you already think you know what it means.
  • Check every assumption you can think of. You think that something is getting stored in a variable? WHO KNOWS?
    • A good way to check your assumptions is to print out anything you can to the Rails server log. Add puts statements to your code!
    • For example: If the jobs count isn't changing when we make jobs, make the jobs controller look like this. Now, it will print to the console the line "In the create method!!!!" and whatever is being returned from Job.create(job_params)
def create
  p "In the create method!!!!!!"
  job = Job.create(job_params)
  p job
  redirect_to jobs_path
end
  • Think about how you would explain the problem to someone else, or, actually explain the problem to another student or a TA!

Next Step: