| Start |
|---|
| Debugging Rails Application To be able to debug an application is very important to understand the flow of an application's logic and data. It helps solving logical bugs and adds value to the programming experience and code quality. Two popular gems for debugging are debugger (opens new window) (for ruby 1.9.2 and 1.9.3) and byebug (opens new window) (for ruby >= 2.x). For debugging
If you want to debug |
| Debugging Ruby on Rails Quickly + Beginner advice Debugging by raising exceptions is far easier than squinting through Debugging Ruby/Rails Quickly:1. Fast Method: Raise an `Exception` then and `.inspect` its resultThe fastest way to debug Ruby (especially Rails) code is to In the above code, This technique is useful for quickly examining an object or method (e.g. is it 2. Fallback: Use a ruby *IRB* debugger like `byebug` or `pry`Only after you have information about the state of your codes execution flow should you consider moving to a ruby gem irb debugger like To use the
This IRB debuggers like Byebug are useful for deeply analyzing the state of your code as it executes. However, they are more time consuming procedure compared to raising errors, so in most situations they should not be your first step. General Beginner AdviceWhen you are trying to debug a problem, good advice is to always: Read The !@#$ing Error Message (RTFM) That means reading error messages carefully and completely before acting so that you understand what it's trying to tell you. When you debug, ask the following mental questions, in this order, when reading an error message:
In the stack trace pay particular attention to lines of code that come from your project (e.g. lines starting with To illustrate why interpreting in this order is important... E.g. a Ruby error message that confuses many beginners:You execute code that at some point executes as such: and you get an error that states:
Beginners see this error and think the problem is that the method
Unfortunately, due to how this error is written ( Summary: Always carefully read the entire error message before beginning any debugging. That means: Always check the class type of an object in an error message first, then its methods, before you begin sleuthing into any stacktrace or line of code where you think the error may be occurring. Those 5 seconds can save you 5 hours of frustration. tl;dr: Don't squint at print logs: raise exceptions instead. Avoid rabbit holes by reading errors carefully before debugging. |
| Debugging in your IDE Every good IDE (opens new window) provides a GUI (opens new window) for interactively debugging Ruby (and thus Rails) applications where you can add breakpoints, watches, auto pausing on exception and allows you to follow the code execution even step by step, line by line. For example, take a look at one of the best Ruby IDE's, RubyMine's debugging features on the picture |
| Debugging ruby-on-rails application with pry pry (opens new window) is a powerful tool that can be used to debug any ruby application. Setting up a ruby-on-rails application with this gem is very easy and straightforward. Setup To start debugging your application with pry
Use Using pry in your application is just including i) Debugging a Controller app/controllers/users_controller.rb In this example, the rails server pauses with a pry console at the break-point when you try to visit a page routing to ii) Debugging a View app/views/users/show.html.haml In this example, the break-point pauses with pry console when the ii) Debugging a Model In this example, the break-point can be used to debug In conclusion, pry is a powerful debugging tool for rails application with easy setup and straightforward debugging guideline. Give this a try. |
Read Article
https://develop.horje.com/learn/1434/reference