<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Michael Daross</title>
    <description>The latest articles on Forem by Michael Daross (@crookedgrin).</description>
    <link>https://forem.com/crookedgrin</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1059503%2F185b1b5d-69ca-43a2-95d2-a3012ab91e5c.png</url>
      <title>Forem: Michael Daross</title>
      <link>https://forem.com/crookedgrin</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/crookedgrin"/>
    <language>en</language>
    <item>
      <title>Finding your way around Rails code</title>
      <dc:creator>Michael Daross</dc:creator>
      <pubDate>Mon, 11 Dec 2023 17:40:58 +0000</pubDate>
      <link>https://forem.com/crookedgrin/finding-your-way-around-rails-code-2k5l</link>
      <guid>https://forem.com/crookedgrin/finding-your-way-around-rails-code-2k5l</guid>
      <description>&lt;p&gt;In Ruby on Rails, it can be really difficult to figure out where you are in the code, since Rails conventions dictate using the same file names over and over (&lt;code&gt;new.html.erb&lt;/code&gt;, &lt;code&gt;show.html.erb&lt;/code&gt;, etc.). Here are a couple hacks to make it easier.&lt;/p&gt;

&lt;p&gt;Note: Parts of this guide assume you're using VS Code, but some of it should work for any IDE.&lt;/p&gt;

&lt;h2&gt;
  
  
  Adding the file path to VS Code tabs
&lt;/h2&gt;

&lt;p&gt;It's hard to tell where you are in the application from looking at the file tab in VS Code. But we can add the path to the tabs to show us the parent folder. Here's how:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Hit &lt;code&gt;CMD+Shift+P&lt;/code&gt; to open the Command Palette&lt;/li&gt;
&lt;li&gt;Begin typing &lt;code&gt;JSON&lt;/code&gt; and navigate to &lt;code&gt;Preferences: Open User Settings (JSON)&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Add this line: &lt;code&gt;"workbench.editor.labelFormat": "short",&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Save&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You have the option to use &lt;code&gt;default&lt;/code&gt;, &lt;code&gt;short&lt;/code&gt;, &lt;code&gt;medium&lt;/code&gt;, or &lt;code&gt;long&lt;/code&gt; paths. &lt;code&gt;short&lt;/code&gt; shows just the parent folder:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fonlmldv1vn2x0hnm8mxg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fonlmldv1vn2x0hnm8mxg.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I find anything more than that to make the tabs way too large, but feel free to experiment.&lt;/p&gt;

&lt;h2&gt;
  
  
  Adding links to the Rails error page
&lt;/h2&gt;

&lt;p&gt;It would be really nice if the error page didn't just give us the file location, but instead created a link that would open the file in the code editor. We can actually modify the error page to do just that.&lt;/p&gt;

&lt;p&gt;First, you'll need to find your Ruby installation. You can do that via &lt;code&gt;gem environment&lt;/code&gt; (or &lt;code&gt;gem env&lt;/code&gt;). This should give you something like the following:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;RubyGems Environment:
  - RUBYGEMS VERSION: 3.4.10
  - RUBY VERSION: 3.2.2 (2023-03-30 patchlevel 53) [arm64-darwin22]
  - INSTALLATION DIRECTORY: /Users/michaeldaross/.asdf/installs/ruby/3.2.2/lib/ruby/gems/3.2.0
  - USER INSTALLATION DIRECTORY: /Users/michaeldaross/.gem/ruby/3.2.0
  - RUBY EXECUTABLE: /Users/michaeldaross/.asdf/installs/ruby/3.2.2/bin/ruby
  - GIT EXECUTABLE: /usr/bin/git
  - EXECUTABLE DIRECTORY: /Users/michaeldaross/.asdf/installs/ruby/3.2.2/bin
  - SPEC CACHE DIRECTORY: /Users/michaeldaross/.gem/specs
  - SYSTEM CONFIGURATION DIRECTORY: /Users/michaeldaross/.asdf/installs/ruby/3.2.2/etc
  - RUBYGEMS PLATFORMS:
     - ruby
     - arm64-darwin-22
  - GEM PATHS:
     - /Users/michaeldaross/.asdf/installs/ruby/3.2.2/lib/ruby/gems/3.2.0
     - /Users/michaeldaross/.gem/ruby/3.2.0
  - GEM CONFIGURATION:
     - :update_sources =&amp;gt; true
     - :verbose =&amp;gt; true
     - :backtrace =&amp;gt; true
     - :bulk_threshold =&amp;gt; 1000
     - "gem" =&amp;gt; "--no-document"
  - REMOTE SOURCES:
     - https://rubygems.org/
 ...
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Note the &lt;code&gt;GEM PATHS&lt;/code&gt; line, which in my case points to &lt;code&gt;/Users/michaeldaross/.asdf/installs/ruby/3.2.2/lib/ruby/gems/3.2.0&lt;/code&gt;. If you navigate to that path in your filesystem, you should see a &lt;code&gt;gems&lt;/code&gt; folder inside. This is where we'll find the files we need to edit.&lt;/p&gt;

&lt;p&gt;Depending on your setup, you might have multiple versions of &lt;code&gt;actionpack&lt;/code&gt; installed. To figure out which one your app is using, type&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;bundle show | grep actionpack&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;In my case, this gives&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;* actionpack (7.1.0)&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;Once you know the folder, in your code editor, open up the error template: &lt;code&gt;gems/actionpack-7.1.0/lib/action_dispatch/middleware/templates/rescues/template_error.html.erb&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Replace line 10:&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Showing &amp;lt;i&amp;gt;&amp;lt;%= @exception.file_name %&amp;gt;&amp;lt;/i&amp;gt; where line &amp;lt;b&amp;gt;#&amp;lt;%= @exception.line_number %&amp;gt;&amp;lt;/b&amp;gt; raised:&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;with&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Showing &amp;lt;a href="vscode://file/&amp;lt;%= @exception.file_name %&amp;gt;:&amp;lt;%= @exception.line_number %&amp;gt;"&amp;gt;&amp;lt;i&amp;gt;&amp;lt;%= @exception.file_name %&amp;gt;&amp;lt;/i&amp;gt;&amp;lt;/a&amp;gt; where line &amp;lt;b&amp;gt;#&amp;lt;%= @exception.line_number %&amp;gt;&amp;lt;/b&amp;gt; raised:&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;This will change the line at the top of the error page into an actual link to open the relevant file in your code editor directly.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6sn6sgulvfwiw53jkigh.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6sn6sgulvfwiw53jkigh.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you’re not using VS Code, here are some other filesystem prefixes you can substitute:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;RubyMine: x-mine://open?file=&lt;/li&gt;
&lt;li&gt;MacVIM: mvim://open?url=file://&lt;/li&gt;
&lt;li&gt;Emacs: emacs://open?url=file://&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Further extending this idea, let’s add links to the other files in the application stack trace.&lt;/p&gt;

&lt;p&gt;In &lt;code&gt;gems/actionpack-7.0.6/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb&lt;/code&gt;, add this line below line 21:&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;| &amp;lt;a href="vscode://file/&amp;lt;%= Rails.root %&amp;gt;/&amp;lt;%= frame[:trace][0...frame[:trace].rindex(":")] %&amp;gt;"&amp;gt;Code&amp;lt;/a&amp;gt;&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;This will add a code link to the right of each file in the  stack trace:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgui83jwaxe1eymdbkk2i.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgui83jwaxe1eymdbkk2i.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Bonus: Adding &lt;code&gt;rails-template-inspector&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://github.com/aki77/rails-template-inspector/tree/main" rel="noopener noreferrer"&gt;Rails Template Inspector&lt;/a&gt; is an awesome debugging tool that allows you to hover over views in your application and see which file is rendering them, and then click to open the file in your code editor.&lt;/p&gt;

&lt;p&gt;The repo includes installation instructions, but here are a few additional steps I took to make it more configurable for my team, some of whom use different IDEs or have different preferences.&lt;/p&gt;

&lt;p&gt;in &lt;code&gt;config/environments/development.rb&lt;/code&gt;, add the following:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; # Annotate rendered view with file names. Necessary for Rails Template Inspector tool.
  config.action_view.annotate_rendered_view_with_filenames = true

  # Options for the Rails Template Inpsector tool loaded in application.html.slim. You can override
  # these locally via environment variables.
  # -----------
  # @url_prefix: The string that launches your code editor from the browser. Options:
  # - Visual Studio Code: vscode://file
  # - RubyMine: x-mine://open?file=
  # - MacVIM: mvim://open?url=file://
  # - Emacs: emacs://open?url=file://
  # - IntelliJ: idea://open?file=
  # - PyCharm: pycharm://open?file=
  #
  #   Example: To use RubyMine, add the following line in .bashrc:
  #     export TEMPLATE_INSPECTOR_URL_PREFIX="x-mine://open?file="
  # -----------
  # @combo_key: The keyboard shortcut to launch the inspector
  # -----------
  # @no_auto_disable: Whether to automatically close the tool after taking you to your code editor
  config.template_inspector_options = {
    url_prefix: ENV["TEMPLATE_INSPECTOR_URL_PREFIX"] || "vscode://file",
    combo_key: ENV["TEMPLATE_INSPECTOR_COMBO_KEY"] || "command-shift-v",
    no_auto_disable: ENV["TEMPLATE_INSPECTOR_NO_AUTO_DISABLE"] || false,
  }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then, to actually show the template inspector, you'll need to  include the &lt;code&gt;&amp;lt;rails-inspector&amp;gt;&lt;/code&gt; web component tag to the page. Our app is using Slim templates, so for us we added the following to &lt;code&gt;application.html.slim&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Adds the Rails Template Inspector tool. See https://github.com/aki77/rails-template-inspector
- if Rails.env.development?
  script type="module" src="https://cdn.skypack.dev/@aki77/rails-template-inspector@^0.5.0"
  rails-inspector[
    root = "#{Rails.root}"
    url-prefix = Rails.configuration.template_inspector_options[:url_prefix]
    combo-key = Rails.configuration.template_inspector_options[:combo_key]
    no-auto-disable = Rails.configuration.template_inspector_options[:no_auto_disable]
  ]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Or, in ERB:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;% if Rails.env.development? %&amp;gt;
  &amp;lt;script type="module" src="https://cdn.skypack.dev/@aki77/rails-template-inspector@^0.5.0"&amp;gt;&amp;lt;/script&amp;gt;
  &amp;lt;rails-inspector 
  root="&amp;lt;%= Rails.root %&amp;gt;" 
  url-prefix="&amp;lt;%= Rails.configuration.template_inspector_options[:url_prefix] %&amp;gt;" 
  combo-key="&amp;lt;%= Rails.configuration.template_inspector_options[:combo_key] %&amp;gt;" 
  no-auto-disable = "&amp;lt;%= Rails.configuration.template_inspector_options[:no_auto_disable] %&amp;gt;"&amp;gt;
&amp;lt;/rails-inspector&amp;gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I hope this saves you some time and frustration. It certainly has for me!&lt;/p&gt;

</description>
      <category>rails</category>
      <category>webdev</category>
      <category>debugging</category>
      <category>vscode</category>
    </item>
  </channel>
</rss>
