Let's take a look at some tips for debugging this time.
First, and most importantly, writing test code, writing unit tests and functional tests in rails is simple, using them to reduce the rate of bugs, or to avoid bugs in your previous code.
The test code tells you what works and what doesn't, and the test code helps you isolate the problematic code, because sometimes the bug doesn't show up quickly.
If the problem occurs in a model, we can use the script Script/console to simulate an IRB conversation, where it is tracked, such as the following:
Z:\study\ruby\instantrails\rails_apps\depot>ruby script/console
Loading Development environment.>> PR = Product.find (: i)
=> #<product:0x4b969ec @attributes ={"Image_url =>" http://www.dearbook.com. >> pr.price
=> #<bigdecimal:4b913d4, ' 0.12351E3 ', 8 (a) >
>> pr.image_url
=> "jpg /lastts00168921__.jpg "
>> pr.save
=> true
>>
Let's start the IRB session with Ruby Script/console on the command line of the depot project, and the blue part is the command I entered in the IRB session.
In addition, the use of log in a complex program is also a good way, you can find a lot of valuable information in the log file, when some unexpected circumstances occur, log file should be the first place you want to check. Also, checking the server log file is also useful, and if you use Webrick as a Web server, you can also use script Script/server to start a session.
We can use the Logger object to output the information we want to see, sometimes log files are so busy that it's hard to find the log you're adding, if you use Webrick, You can use stderr to output your log to the console (Instantrails use mongrel cannot enter the command line).
If a page displays the wrong content, we may want to export the object in the controller, and the method debug () can help us to format the object's contents well and to determine if the object's contents are not legitimate HTML, for example:
Finally, when the above methods do not exclude bugs, we try to track the debugger, but only for environments configured as development, and here are the steps for using breakpoints (breakpoint):
1. Add a call to the breakpoint () method where you want the program to stop.
2. Type the command at the root of the project: Depot>ruby Script/breakpointer.
Open the browser so that your program can hit the code that calls the breakpoint method, when the browser stops responding, and in the command-line window opens the IRB session, where you can view the value of the variable or add a breakpoint, and when you launch the IRB session, the program continues to run in the browser.