Jakaŭ Krasnoŭ

October 21, 2023

How to make ruby-lsp-rails work with your dockerized Rails app

I recently watched Aaron Patterson's keynote at Rails.world, where he introduced a gem called ruby-lsp-rails, an extension of ruby-lsp. This extension goes a step further by incorporating knowledge of Rails internals and providing additional valuable information, such as model fields when you hover over model class.

Here's how it works, assuming you already have ruby-lsp up and running: You can simply add ruby-lsp-rails to your Gemfile. This action adds a "/ruby_lsp_rails" endpoint to your application. Yes, your server needs to be up and running for this to work, but that's essentially all you have to do, and it should function seamlessly.

However, there's a small catch, especially if you have a non-standard setup. In our case, we run our development environment inside docker containers using docker compose, and it's available on localhost:80 but hidden behind a HAProxy, which we use for routing requests to multiple applications. The documentation, at least at the time, didn't address this particular setup. Nonetheless, I came across an issue and examined the associated pull request.

As it turns out, you can specify a custom host using "tmp/app_uri.txt." I placed http://localhost:80 in this file, and it started working. However, our setup also utilizes HAProxy for request routing. By default, "/ruby_lsp_rails" was leading to our React app, resulting in a 404 page. To address this, I added a path to the proxy rule, and everything began to function as intended.

In conclusion, ruby-lsp-rails offers a fantastic enhancement to your Ruby on Rails development experience. Whether you're using a standard setup or have unique configurations like ours, it's worth exploring and adapting to your needs.