These guides are still a work in progress and may change as we improve the documentation. Please check back later for updates.
A smooth developer experience starts with great debugging tools and fast feedback loops. This guide walks you through setting up essential tools such as ruby-debug, BetterErrors, Web Console, and Hot-reloading with Foreman/esbuild.
Rails 7+ ships with the debug gem (successor to byebug). It’s already in
your Gemfile, but here’s a refresher on usage.
# Set a breakpoint anywhere in your code
debugger
# Then run your server (or tests) and execution will pause hereWhen execution stops, you can inspect variables, step through code, and even evaluate Ruby expressions.
If you are using the LocoMotion Docker setup, Rails will automatically start
a remote debugger.
After adding a debugger breakpoint and refreshing the browser, the request
will appear to hang. Open another terminal and run:
make app-debug
This command attaches to the remote debugger inside the container so
you can step through code just like you would locally.
binding_of_callerFor richer error pages, add the following gems to your development group:
# Gemfile
group :development do
gem "better_errors"
gem "binding_of_caller"
endAfter running bundle install, restart your Rails server. You will now see
an interactive stack trace page whenever an exception is raised.
When running inside Docker, BetterErrors will refuse connections from
the host machine. Make sure to add the following to
config/environments/development.rb:
# Allow BetterErrors to render
BetterErrors::Middleware.allow_ip! "0.0.0.0/0"Restart your container (e.g. make app-restart) and refresh the page to
get the fully-featured error screen.
BetterErrors is development-only. It should never run in production.
Rails ships with Web Console which
inserts an IRB prompt right into your browser whenever an exception is
raised or when you call the console view helper.
It is included by default in new Rails applications and enabled in the
development environment.
When Rails is executed inside Docker you will usually see a line like this in the logs:
Cannot render console from 172.23.0.1! Allowed networks: 127.0.0.0/127.255.255.255, ::1Add the following to config/environments/development.rb:
# Fix console permissions for Docker so Web Console works no matter the IP address
config.web_console.permissions = "0.0.0.0/0"Restart the app (using make app-restart) and refresh the page – the
interactive console should now appear whenever there is an error.
To use Web Console manually inside a view, call the console helper.
- # You can access the context at the point `console` is called
- my_var = 42
- console # Opens a live console in the rendered view (my_var will be available)For a tight feedback loop on JS/CSS changes we recommend running a
Procfile with Foreman (or bin/dev from Rails 7):
# Procfile.dev
web: rails server -b 0.0.0.0
js: esbuild app/javascript/*.* --bundle --watch
css: tailwindcss -i ./app/assets/stylesheets/application.tailwind.css -o ./app/assets/builds/application.css --watchAnd for hot reloading (so you don't even have to refresh your page), just
add the hotwire-livereload
gem to your Gemfile (in the development group).
# Gemfile
group :development do
gem "hotwire-livereload"
endThen start everything with:
bin/dev
Your browser will auto-refresh as you edit any of your files (HAML, JS, CSS, etc)!
Rails.logger.debug to print structured output.rails logs -f
make dev or prune Docker caches.
Create virtual credit / debit cards to keep your real info safe.
Get $5 when you sign up — free to start!
Everything you need to grow your business with confidence!
CRM, Lead Generation, Project Management, Contracts, Online Payments, and more!
The ads above are affiliate links to products I regularly use and highly
recommend.
I may receive a commission if you decide to purchase.