Simple methods to debug when developing in rails.


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:

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

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

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

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
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.