Simple Debugging In Rails
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
:
"$2a$10$S3H9mBLQmfkBdaV3j/4M8ud7te9DwDwrreklqwjg4jt63oh528adv"
- The Idiomatically Correct Way To Make An Instance Of A Many To One Relationship ModelAugust 18, 2016
- Simple Debugging In Rails
- Common Steps To Start A Rails ProjectAugust 8, 2016
- Building A Hello World App In Ruby On Rails AppAugust 7, 2016
- Ruby On Rails OverviewAugust 7, 2016
Articles
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
·