DEV Community

Kevin S.
Kevin S.

Posted on

Open Ruby on Rails errors with VS Code

Hey everyone! Today, I want to share with you a trick that has made my life as a Ruby on Rails developer much easier and more productive. Have you ever found yourself staring at this error screen and feeling like you're wasting time searching for the error file?

Error Screen

Well, here's a solution to cut down those minutes of searching and boost your productivity. Instead of manually navigating through files in VS Code to find the error file, we can do it with just a click!

First, open your terminal and run the following command:

bundle show actionpack
Enter fullscreen mode Exit fullscreen mode

This command will give you the path where the actionpack library is located on your system. In my case, the path is /Users/kevin/.rbenv/versions/3.3.0/lib/ruby/gems/3.3.0/gems/actionpack-7.1.3.2.

Next, open this directory in VS Code and find the _trace.html.erb file. Once you've found it, add the following code right after the <a> tag with the class "trace-frames":

<%= link_to "Open in VS Code", "vscode://file#{Rails.root}/#{frame[:trace].split(": ").first.split(":in").first.strip}", style: "font-weight: 700; color: #0078D7; margin-left: 0.5rem;" if Rails.respond_to?(:root) && name == "Application Trace" %>
Enter fullscreen mode Exit fullscreen mode

It should look something like this:

Added Code

This code dynamically generates a link that, when clicked, opens the error file directly in VS Code. It's a handy shortcut that saves you from manually searching for the error file every time.

Save the changes and restart the Rails server. That's it!

Now, if you refresh the error screen you'll see a blue link that says "Open in VS Code". Simply click on it, and you'll be taken directly to the file and line where the error happened!

Link on Error Screen

It's that easy!

I hope this trick helps you save time and makes you more productive in your work with Ruby on Rails! If you have any questions or comments, feel free to share them below! 😊

Top comments (1)

Collapse
 
podcastlaunch profile image
Podcast Launch Strategy

Awesome post