Simple Debugging In Rails

Published:
Last modified:
Tags Rails , Debugging

Overview

There are two very simple ways to debug a rails app:

  • printing debug information directly in a view
  • Using byebug gem

Debug info in views

Adding the following line to /app/views/layouts/application.html.erb to show debugging information in each view only in the development environment:

...
<body>
...
<%= debug(params) if Rails.env.development? %>
</body>

Byebug gem

Rails 5 has the byebug gem included, adding debugger to any controller action, makes it possible to interact with the application in the console server each time a view is accessed in the browser. Adding debugger in a controller action:

class UsersController < ApplicationController
  def new
  end

  def show
    @user = User.find(params[:id])
    debugger
  end
end

Then accessing a url of the application, the rails server shows the (byebug) prompt so the developer can inspect current view variables values, for example when accessing http://localhost:3000/users/1:

(byebug) @user.password_digest
"$2a$10$S3H9mBLQmfkBdaV3j/4M8ud7te9DwDwrreklqwjg4jt63oh528adv"
Uruguay
Marcelo Canina
I'm Marcelo Canina, a developer from Uruguay. I build websites and web-based applications from the ground up and share what I learn here.
comments powered by Disqus


Except as otherwise noted, the content of this page is licensed under CC BY-NC-ND 4.0 . Terms and Policy.

Powered by SimpleIT Hugo Theme

·