Troubleshooting Your Apps

As you go forth into the wide and exciting world of app development, enjoying all the fruitful satisfaction that it contains, you will no doubt encounter issues (bugs) that you just can't get to the bottom from simply looking at your code. Don't feel embarrassed by this, it happens to the best of us - so much so that by default Rails installs a gem to help squash these bugs; aptly named byebug.

Let's use a relevant, non-silly example of something that might catch you out.

Say you are building a web app to store particularly excellent photos of cats. Your database is populated, your MVC components set up and you try to navigate to one of your lovely pictures on your app but see the following depressingly cat-free page instead:

Rails error page

Now, some / most of us will have seen what the problem is immediately here. But for the sake of showing you the basics of byebug, let's pretend we're none the wiser.

We can use byebug here to interrupt the running Rails server and drop into an interactive session at the specific point it is called. To do so, let's open up our controller and drop the line into the show action just before the instance variable is defined:

class CatController < ApplicationController
  
  def show
    byebug   ## this is all we need to do
    @cat = Cat.find(params[:ID])
  end

end
This is all that is needed to enable the functionality byebug offers us - no arsing around with importing new gems because it already exists in the Gemfile!

Now if we refresh our browser page, we will see it throb as though it is hanging. However, if we head to the terminal screen that we are running the Rails server in we will see it has opened up the byebug utility with some information as to where specifically it is being called:

[1, 8] in /home/nask/projects/catzrnice/app/controllers/cats_controller.rb
   1: class CatsController < ApplicationController
   2: 
   3:   def show
   4:     byebug
=> 5:     @cat = Cat.find(params[:ID])
   6:   end
   7: 
   8: end
(byebug) 
From here we can essentially do everything we can from the Rails console, but from the scope of the current state of the application. For example, if we call params:
(byebug) params
#<ActionController::Parameters {"controller"=>"cats", "action"=>"show", "id"=>"1"} permitted: false>
we can see exactly what the hash contains at this particularly point in the session.

In this particular instance, it also allows us to see exactly what the problem is: the parameter key for ID is id, however in our controller we are trying to define the instance variable with params[:ID]!
We can verify this by attempting to run the line next line that is in the controller from the byebug interface:

(byebug) @cat = Cat.find(params[:ID])
   (0.3ms)  SELECT sqlite_version(*)
  ↳ (byebug):1:in `show'
*** ActiveRecord::RecordNotFound Exception: Couldn't find Cat without an ID
to confirm it is the same error given by the Rails error page we saw in the browser.

In this instance we can ensure that this is all that is wrong by explicitly definining params[:ID], and telling the server to resume its process by entering continue:

(byebug) params[:ID] = '1'
"1"
(byebug) continue
  Cat Load (0.7ms)  SELECT "cats".* FROM "cats" WHERE "cats"."id" = ? LIMIT ?  [["id", 1], ["LIMIT", 1]]
  ↳ app/controllers/cats_controller.rb:5:in `show
whereupon we will see our browser page successfully load into the page we were expecting!

Now that we know what is wrong and how to fix it, we can go into our controller and fix the offending line (removing byebug in the process):

class CatController < ApplicationController
  
  def show
    @cat = Cat.find(params[:id])  ## ID becomes id to match what Rails is expecting
  end

end
and refresh our browser page again to see our lovely app working exactly as it should!

Successful render of cat page

And there we have: an incredibly powerful but easy tool to use to dig our way out of the most monumental of holes. For those already familiar with Ruby, it's basically pry for Rails. Additional details of what byebug is capable of can be found on its GitHub repo page, and I certainly recommend checking out the rest of what the extremely clever chap who built it has also been building.

 
 

Comments

Popular posts from this blog

New Rails Apps with Docker Compose

[ToDoList] Basic Pages

[ToDoList] Docker Compose