visit
Debugging in a docker container isn't that straightforward. At least it is not in the way my team set everything up. For starters, debugger
does not work inside a container. I also had to struggle for the first month because binding.pry
usage was not that straightforward to me.
TDD is the golden standard and it works great in a Rails container.
It works by placing binding.pry
inside your code and running the tests. The tests should stop at the statement in the code. If it doesn't, then you know that the binding.pry
method was not reached. I like this method because I am bad at writing tests. I can debug my tests or my code with it.
TDD is the way to go, but it is practically impossible to cover everything with tests. This is a trick that I love to use when I want to check out something quick. It works by placing a raise method at the desired stopping point. The raise method takes as an argument the object you would like to check and send it the .inspect
message.
For exampleraise name_of_your_method_or_variable.inspect
. Example below:
Since your code is inside a docker container, nothing will happen in the browser window. The only way to see your error is by going to your browser's Inspect -> Network. Repeat the request that you know will go through the function you placed the raise statement in. Make the request once and see the error displayed in the Preview tab.
This is a nifty trick that can save you a lot of time. Suppose you are working on a feature in an application and you have to fill out a lot of required fields. When you press submit, your fields are being emptied. You would have to refill all those fields just to test once. That is a big waste of time. In the Chrome Inspection tool inside the Network tab, you can make a request once. Placing your mouse over the response, right-clicking on it and choosing replayXHR. The browser will repeat the request.
I was often told to read the backtrace, yet no one had explained what it meant. It can be intimidating when you don't know the ins and outs of a Rails application. Reading it is actually very straightforward, from bottom to top. Going through the backtrace you stop at the methods of the file that you modified and looks familiar. Other than the migrations they are usually at the top. You get a lot of valuable information not only the filename name of the method but also the line number.
For some reason, binding.pry
doesn't stop execution in a container. Our team is using another gem called pry-remote. You would put binding.remote_pry
wherever you would normally put binding.pry
. Making the request, the app will look like it is being loaded, but nothing will happen. Running pry-remote in the shell of the rails container will then connect to the session. You will be able to interact with pry as you normally would.