Make The Form Work

Goals

  • Make the form actually work

  • Learn how to read server output

  • Review happy debugging techniques

  • Partytime: Use the form to Create Objects

Steps

Step 1: Try to submit form

Error! Woo!!!

The action 'create' could not be found for JobsController

what is the next step?

Step 2: add the action create to the JobsController

So, let's add it:

def create
end

Try to use your form again. Unfortunately

This time, there won't be any output, not even an error page. So now we need a new way to investigate

Step 3: debug your app using the server output

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".

Go over the output of the server, talking through the various pieces of information you see.

Then try to submit the form again, but now look at the output from the server.

In the output from your Rails server you might find this:

Error! Woo!!!

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

Familiar error, right? We don't have a template called create.html.erb.

BUT!!! This time we're not going to make one.

We want to store the new job in the database, and afterward go somewhere useful: back to the main jobs page, so we can immediately see there is one more job in the list now.

We need to tell the create method where to go, instead of just loading the default view.

Step 4: redirect back to jobs

Add this code to your jobs controller.

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.

Step 5: save the 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: {
  "authenticity_token"=>"[FILTERED]",
  "job"=>{
    "title"=>"organizer organizer",
    "description"=>"We need an someone 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! 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!)

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!

Step 6: find the jobs on the rails console

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! You have successfully built a form that saves data to the database

Tips for Effective Debugging

If your jobs do not land in the database, it's time to debug!

  • 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

Now would be a good time to commit to git

Next Step: