Debugging a Ruby On Rails Application in Visual Studio Code
Kanani Nirav
Posted on August 12, 2022
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 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.
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
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.
If you have launch.json then click ‘Add configuration’
select the ‘Ruby: Rails server’ option. It will add config something like this:
launch.json
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.
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.
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.
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!
Posted on August 12, 2022
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.