Debugging a Ruby On Rails Application in Visual Studio Code
Tags: vscode, ruby, rails, productivity
Published: 2022-08-12 00:15:00


VS Code Debugger for Ruby on Rails

I have been using Visual Studio Code as my IDE while coding in Ruby as I didn’t want to fork out a load of money to purchase RubyMine and so far have been getting by without being able to debug my code. Now the only reason I haven’t needed to debug so far is that I am still learning Ruby and Rails and therefore the code I am writing isn’t exactly the most difficult to fix when something goes wrong. But I got stuck recently and it took me a long long time to figure out what was going wrong and started to wish that I had a debugger setup.

Setting it up is actually really easy as some awesome people have made some plugins and gems for us to use.

So the first thing to do is install the [Ruby plugin](https://marketplace.visualstudio.com/items?itemName=rebornix.Ruby) in VS Code. This plugin does much more than just enabling debugging but I’ll let you find that out for yourself, the information on the plugin’s page is a good start.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6sdfu7z64i93qzvnxcol.png)

Once that is done we need to install the gems that actually do the debugging that the IDE can use to display. Depending on the version of Ruby you are using you will need to install slightly different gems.

    # Ruby 1.8.x
    gem install ruby-debug-ide
    gem install ruby-debug-base</pre>

    # Ruby 1.9.x
    gem install ruby-debug-ide
    gem install ruby-debug-base19x</pre>

    # Ruby 2.x
    gem install ruby-debug-ide
    gem install debase

    # Ruby 3.x
    gem install ruby-debug-ide
    gem install debase

Once it’s done, we need to add configuration in our *launch.json*.

So go to the debug tab on the left in VS Code. Need to add configuration in *launch.json* file for Ruby.

**If you don’t have click on create launch.json link as slow below**

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5l8d9v3bxt1ua7a1rhmc.png)

After clicking on create launch.json link you can see a drop-down then select first Ruby and then in the next drop down select Rails server It will generate the file for you.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/eu0y55bnsssqe05s6v0h.png)

**If you have launch.json then click ‘Add configuration’**

select the ‘Ruby: Rails server’ option. It will add config something like this:

**launch.json**
{% gist https://gist.github.com/kananinirav/6b457b930e200f71e7859a31c3178c16.js %}
If you want to use a port other than the default port (3000) for the rails server then you can specify that in arguments like this

    "args": [
        "server",
        "-p",
        "3100"
    ]

Now if you click Run with the Rails server option selected in the drop-down it will start up in debug mode and a little menu will appear at the top of the screen with debug options such as continue and step over.

Now that the server has been started in debug mode we can add some breakpoints to step into the code and see what is happening. So I have put a breakpoint into my code and let's see what happens.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7y6iv00g4fsw9lnm3nrn.png)

Now if I call this method it will pause when it reaches the breakpoint. If you were in a browser it should open VS Code and wait for your next move.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/74tdty5kjp3wda57fhxn.png)

At this point, you can decide to just carry on normal execution by pressing continue (f5) or step over (f10), there are a few more options. Another decision you can make is to have a look at the state of variables and much more information just by looking to the left of the screen. Below is an image of what the variables section should look like.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/c8or70jk13mi0r3p7fjr.png)

You can also watch a variable or object.

So that is the end of this article on debugging a Rails server with Visual Studio Code. By enabling debugging we can make this already useful (and free!) IDE is even better. Happy debugging!

If you just want to get an overall picture of the code behavior in hopes that may help you find out where things are going wrong? AppMap is a tool that can help with that, and here's a post here on dev.to about how to use it - 
https://dev.to/appland/how-to-watch-your-code-interacting-with-rails-5894




If this guide has been helpful to you and your team please share it with others!