---
# System prepended metadata

title: How to Debug and Step-Through Rails Codebase

---

**How to Debug and Step-Through any Rails gem Codebase**

Highlight
### Problem Statement:

- *Ever find yourself in a situation where you are looking at an error resulting from a ruby gem?*
- *Does source code intimidate you, thinking to your self where the heck is this?*
- *Do you wake up at night not knowing how to debug your code further and feel stuck?...AHHHH*

### Context Statement:
- *In this BrownBag Session we are going to demystify this obstacle and show you it's not that complicated to look into any rails gem source code and demonstrate how you can do that yourself and impress your junior developers on the team LOL. Code along if you want.*

**This is an interactive code along.**

**Let take a peek at the Rails source code for a deeper understanding of the framework.**


### Step 1: Open the source file

To open any relevant gem, we will need to run this `bundle open gem_name` command in the terminal. 

Note: prerequisite You may have to confi your `~/.zshrc`

``` 
if [[ -n $SSH_CONNECTION ]]; then
   export EDITOR='vim'
 else
   export EDITOR='code .'
 fi
```
 
```$ bundle open actionpack```


### Step 2: Insert a ManMade Error

open : `lib/action_controller/metal/request_forgery_protection.rb:289`

```
def verified_request? line 290
  
  return false (I placed this here)
  
  !protect_against_forgery? || request.get? || request.head? ||
    (valid_request_origin? && any_authenticity_token_valid?)
end
```

### Step 3: Debuging Stage breakpoint

Let us run this line in caseflow as if we have not cule.

`make one-test spec/controllers/hearings/schedule_periods_controller_spec.rb:168`


When debugging any method, insert a breakpoint from debug gem e.g `binding.pry`. Rails includes many out of the box. For any other codebase, you can run bundle add `debug` to install or add `pry` to your gemfile. Once set up, you can insert a breakpoint using the debugger statement at the beginning of the method.


1- We see an error `ActionController::InvalidAuthenticityToken`
2- We google and investigate `ActionController` is part of `ActionPack` gem
3- We open the actionpack gem`bundle open actionpack`
4- We search for `InvalidAuthenticityToken`
5- We class is defined in the `RequestForgeryProtection` module line:8.
6- We notice it being user by `def handle_unverified_request` method line:210
7- We place `binding.pry` here.
8- we notice that `handle_unverified_request` is being called by `def verify_authenticity_token` on line 229
9- We place `binding.pry` notice `!verified_request? => true`
10- We look at `def verified_request?` line 290
11- We see `return false` we added.


### Step 4: Step-through the Method

At this point, you are inside the Rails codebase. You can step through the entire Rails source code as you'd in your regular application.

Stepping
To step through the code, you can use the following commands:

- `whereami`: when you get lost in the code
- `break`: Manage breakpoints.
- `step`: Step execution into the next line or method. Takes an optional numeric argument to step multiple times.
- `next`: Step over to the next line within the same frame. Also takes an optional numeric argument to step multiple lines.
finish: Execute until current stack frame returns.
- `continue`: Continue program execution and end the Pry session.
Callstack navigation
You also can move around in the callstack with these commands:

- `backtrace`: Shows the current stack. You can use the numbers on the left side with the frame command to navigate the stack.
- `up`: Moves the stack frame up. Takes an optional numeric argument to move multiple frames.
- `down`: Moves the stack frame down. Takes an optional numeric argument to move multiple frames.
- `frame` <n>: Moves to a specific frame. Called without arguments displays the current frame.


**Step 5: Step-through the Method**

- Please don't forget to remove any added code in your gems files.
