Ruby on Rails development from scratch (Windows) (36)-Debugging Tips

Source: Internet
Author: User
Tags command line ruby on rails

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.