💡

7 essential tools to improve your Rails DX

Ruby on Rails VSCode DX

The Ruby on Rails framework is renowned for its developer-friendly ecosystem, but you can further elevate your work experience with these 7 essential tools.

1. Maintain code quality with Rubocop integration for Visual Studio Code

RuboCop is Ruby's most popular code linter. It normally works in your terminal. But integrating Rubocop into VSCode streamlines the code review process by highlighting issues directly within the editor, in real time. This keeps your codebase clean with much less effort.

  1. Install Rubocop in your project:

    # Gemfile - under development/test - don't forget to run 'bundle' afterwards
    gem 'rubocop', require: false
    gem 'rubocop-rails', require: false
    gem 'rubocop-performance', require: false
    
  2. Limit default interventions

    I don't like working with autoformatting in Rails models and controllers so I don't add any particular configuration for that. For my projects, Metrics and other Style offenses are not very useful so I turn them off in a rubocop.yml file I create in the root directory. It's available here for the sake of brevity.

  3. Finally, install VSCode's Rubocop extension

That's it, no need to execute Rubocop in the terminal now!

rubocop Suggested corrections and refactoring solutions straight into your editor!

2. Reduce noise logging with Lograge

Lograge is a powerful gem that helps you simplify the log output, making it more human-readable and concise. There are many things you can do with Lograge, but the one I find most useful is slimming down Active storage log entries :

In your Gemfile:

gem "lograge"

Enable it in an initializer or the relevant environment config:

Rails.application.configure do
  config.lograge.enabled = true
  #mute active storage log entries
  config.lograge.ignore_actions = [
    "ActiveStorage::DiskController#show",
    "ActiveStorage::RepresentationsController#show",
  ]
end

This will prevent this giant blob from appearing in your logs :

blob What you can get rid of

3. Fine-tune performance with RackMiniProfiler

RackMiniProfiler allows you to measure and optimize your application's performance, piece by piece. This tool provides real-time feedback on database queries, rendering times, and external requests, enabling you to identify potential bottlenecks. To install :

  # Gemfile - under development (but can run in production) don't forget to run 'bundle' afterwards
  gem 'rack-mini-profiler'

It's pretty intuitive to use so I won't go into details but here are some nice tips :

  • To turn the profiler off, use disable : http://localhost:3000/posts?pp=disable
  • To turn the profiler on, use enable : http://localhost:3000/posts?pp=enable
  • You can also use Alt + P to toggle on Windows/Linux and Option + P on MacOS!
  • pp=help displays help screen with all the available commands

miniprofiler

4. Tidy up your templates with ERB Beautify

VSCode's most popular HTML beautifiers (like Prettier) don't work well with ruby. That's why I use ERB Formatter/Beautify. It's the only solution I found that properly autoformats ERB files, a huge time saver. Installation is straightforward :

  1. Install the extension in VSCode

  2. Add to your Gemfile:

    gem 'htmlbeautifier'
    
  3. In VSCode add this setting in your settings.json file:

    "[erb]": {
      "editor.defaultFormatter": "aliariff.vscode-erb-beautify",
      "editor.formatOnSave": true
    },
    "files.associations": {
      "*.html.erb": "erb"
    }
    

And voilà, this will automatically indent and format your ERB templates!

5. Easily manage environment settings with Dotenv-Rails

Accessing environment variables (such as API keys, database credentials, and other configuration settings) in Rails is not always the easiest thing. We all know you can write your credentials by doing :

EDITOR=code rails credentials:edit

But then reading them might be done with ENV.fetch('API_KEY'), ENV['API_KEY'] and Rails.application.credentials.api_key, depending on the situation. I've always found it confusing, and of course each environment needs a yml config file, and once deployed everything works differently in your server.

dotenv-rails simplifies the process of configuring sensitive information for small Rails applications. Add this line to your Gemfile:

# in groups: [:development, :test]
gem 'dotenv-rails'

Then create an .env file at the root of your project's directory and start adding env variables!

USER=james_bond
SECRET_KEY=moneypenny

You can use that information in Rails because foreman is aware of the .env file and will load it when our application initiates.

puts "Hello #{ENV['LOGIN_USERNAME']}, your codename is #{ENV['SECRET_KEY']}"

Ensure that the .env file is added to your project's .gitignore file to prevent it from being committed to version control. Each developer on your team should create their own local .env file with the necessary environment variables.

6. Accelerate your migration workflow

Rails Latest Migration is a really simple VSCode extension that allows you to quickly go back to your migrations from VS Code Command Palette. Migration files are named with numbers, so they're not easy to search with your editor's file explorer. And being able to reach your migrations rapidly is very useful because you often have to create migrations, migrate and then sometimes roll back. That back and forth movement when you are modeling your database can be time consuming and "RLM" addresses just that problem!

rlm The command that makes you reach your migrations in milliseconds

7. Untangle your JS controllers with Stimulus LSP

This one I found about very recently but it's pretty stellar for interacting with Hotwire's Stimulus - which I use to write almost all of the JS parts in Rails. When writing Stimulus controllers, it's quite easy to make syntax mistakes and not be able to log a proper error your console.

Stimulus LSP tackles that problem by providing intelligent code completion, error checking, and documentation popups directly within VSCode! A game changer, and it just works with a 1-click-install.

stimulus-lsp No more Stimulus syntax errors with this LSP auto-completion!

🧃 Bonus

If you're interested in all the VSCode exentions and configurations I use for Rails, I wrote a note on the this topic.