Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
August 12, 2022 12:15 am GMT

Debugging a Ruby On Rails Application in Visual Studio Code

VS Code Debugger for Ruby on Rails

I have been using Visual Studio Code as my IDE while coding in Ruby as I didnt 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 havent needed to debug so far is that I am still learning Ruby and Rails and therefore the code I am writing isnt 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 Ill let you find that out for yourself, the information on the plugins page is a good start.

Image description

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.xgem install ruby-debug-idegem install ruby-debug-base</pre># Ruby 1.9.xgem install ruby-debug-idegem install ruby-debug-base19x</pre># Ruby 2.xgem install ruby-debug-idegem install debase# Ruby 3.xgem install ruby-debug-idegem install debase

Once its 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 dont have click on create launch.json link as slow below

Image description

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

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.

Image description

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

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

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 this guide has been helpful to you and your team please share it with others!


Original Link: https://dev.to/kanani_nirav/debugging-a-ruby-on-rails-application-in-visual-studio-code-20fk

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To