Debugging the Controller

Debugging is an important part of getting your code to work, and today we got some more practice. We were given a simple Rails app and had to figure out what was wrong with it. The issue with the app is that when a new game is created, the name field remains blank.

To solve this, I broke it down into a couple of basic steps. I started up a local server to test out the app and see firsthand what its behavior was. Based on the initial description, I figured the issue would be either in the view or the controller. When I went through and created a new game in the app, I realized that the name wasn’t displayed on the show page or on the index, and so I figured it was more likely that the bug not be in the view, since a similar bug would have had to be made in both views.

Next I turned my sights to the controller.

Because games were being created, but with blank values, I checked the create action to make sure that params[:game] was being passed to Game.new. I knew this should be the correct param, since the model we’re using that inherits from Active Record is Game, not Games, and we’re creating an instance of that. And this was where I found my error. params[:games] was used in the create action, and params[:game] didn’t exist, so the fields for the game were saved as blank entries.

If you are ever unsure of what the params hash contains and want to see just what the create action is receiving, you can use the debugger gem, or make a view for the create action instead of redirecting to a show view. This view would simply have to use erb or haml to display params, and then the contents of the params hash will be displayed in the browser when you create a new entry in the database.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>