goals do
goal "Make the form actually work"
goal "Learn how to read server output"
goal "Review happy debugging techniques"
goal "Partytime: Use the form to Create Objects"
end
steps do
step "Try to submit form" do
error_box "The action 'create' could not be found for JobsController"
message "what is the next step?"
end
step "add the action create to the JobsController" do
message <<-MARKDOWN
So, let's add it:
MARKDOWN
source_code :ruby,
<<-RUBY
def create
end
RUBY
message "Try to use your form again. Unfortunately"
message "This time, there won't be any output, not even an error page. So now we need a new way to investigate"
end
step "debug your app using the server output" do
discussion_box "Logging & Server Output",
<<-MARKDOWN
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.
MARKDOWN
message "In the output from your Rails server you might find this:"
error_box "No template found for JobsController#create, rendering head :no_content"
message <<-MARKDOWN
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.
MARKDOWN
end
step "redirect back to jobs" do
source_code_with_message "Add this code to your jobs controller.", :ruby,
<<-RUBY
def create
redirect_to jobs_path
end
RUBY
message <<-MARKDOWN
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.
MARKDOWN
end
step "save the form data" do
message "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:"
source_code :http,
<<-JSON
Parameters: {
"authenticity_token"=>"[FILTERED]",
"job"=>{
"title"=>"organizer organizer",
"description"=>"We need an someone to organize the organizers."
},
"commit"=>"Create Job"
}
JSON
message <<-MARKDOWN
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.
MARKDOWN
source_code_with_message "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!)", :ruby,
<<-RUBY
def create
Job.create(job_params)
redirect_to jobs_path
end
private
def job_params
params.require(:job).permit(:title, :description)
end
RUBY
message <<-MARKDOWN
Walk through this code line by line!
MARKDOWN
end
step "find the jobs on the rails console" do
console_with_message "Rails console time! Open up another tab or window in your terminal and type this:", "rails c"
console_with_message "After that's loaded, let's see how many jobs we've saved to the database:", "Job.count"
message <<-MARKDOWN
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
MARKDOWN
end
end
message <<-MARKDOWN
# 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)`
MARKDOWN
source_code :ruby,
<<-RUBY
def create
p "In the create method!!!!!!"
job = Job.create(job_params)
p job
redirect_to jobs_path
end
RUBY
commitnow
next_step "crud_and_resourceful_routing"