DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Coding
  3. Tools
  4. Debugging a Rails Server in Visual Studio Code

Debugging a Rails Server in Visual Studio Code

In this post we take a look at how you can setup your Visual Studio Code installation to debug a Rails server. Sound difficult? It isn't. Read on to find out more.

Dan Newton user avatar by
Dan Newton
·
May. 17, 17 · Tutorial
Like (2)
Save
Tweet
Share
11.83K Views

Join the DZone community and get the full member experience.

Join For Free

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 (I hate spending money) 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 on 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. Anyway, time for me to stop blabbing on and actually give you some information.

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 that just enabling debugging, but I’ll let you find that out for yourself, the information on the plugin’s page is a good start.

Ruby plugin

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


Ruby 1.9.x
gem install ruby-debug-ide
gem install ruby-debug-base19x
Ruby 2.x
gem install ruby-debug-ide
gem install debase


Once you have installed the gems for the version of Ruby you are using you are pretty much there, now you just need to run the Rails server in debug mode and it's done.

Go to the debug tab on VS Code and click the gear icon. That will open the launch.json file which should have a load of configurations marked with type: "Ruby" and, if there aren’t any for some magical reason, then you can add them manually. In case something weird happens to you the configuration for debugging a Rails server is found below.


{
  "name": "Rails server",
  "type": "Ruby",
  "request": "launch",
  "cwd": "${workspaceRoot}",
  "program": "${workspaceRoot}/bin/rails",
  "args": [
     "server"
  ]
}


That should do it! 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. The bar at the bottom of the window even changes color to indicate it is in debug mode.

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 (please don’t make any comments on my code!) and let's see what happens.

Rails before debug

If you don’t know how to add a breakpoint, just click to the left of the line number that you want it on and then the dot will appear like in the image above. I am pretty sure the dot will always be red, but I am not 100% sure that it can’t be changed through themes.

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.

Rails after debug

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, but I’m too lazy to write them down. 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.

Rails after debug 2

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

Visual Studio Code code style

Published at DZone with permission of Dan Newton, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Why It Is Important To Have an Ownership as a DevOps Engineer
  • Handling Virtual Threads
  • How to Use MQTT in Java
  • Integration: Data, Security, Challenges, and Best Solutions

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: