openSUSE:WebYaST tips

Jump to: navigation, search

Debugging Ruby on Rails application


Ruby has an integrated debugger similar to gdb, .

Installing Ruby debugger

Ruby debugger is available in ruby-debug Ruby gem. It can be installed by command

 sudo gem install ruby-debug

It will also install all dependent Ruby gems. (Note: an RPM is not available at the moment).

Starting the debugger

Starting from a Rails application

At first, the place in the application where the debugger will be started must be marked by debugger keyword.

Then the application must be started in the debugging environment - it is simple, just use --debugger option of the starting script, e.g. use

 script/server --debugger

command to start the Rails application, the output will look like:

 => Booting WEBrick
 => Rails 2.3.2 application starting on http://0.0.0.0:8080
 /webservice/config/environment.rb
 Registering plugin resources
 => Debugger enabled
 => Call with -d to detach
 => Ctrl-C to shutdown server


Starting at Rails start up

The debugger is sometimes needed even before your Rails application is loaded, e.g. if the problem occurs while loading Rails components. In this case use command

 rdebug script/server -- -p 3000 --debugger

to start the application in the debugger directly. (Note the double dash string which ends rdebug options.)

The Rails loading code is quite complicated and you should use the first way if possible, otherwise you will spend too much time going through the huge Rails loading code.

Using the debugger

When the application executes debugger command it stops and a debug session is opened in the terminal where the application has been started.

The most important basic commands:

l (line) - print the source script (l= prints the current line)
c (continue) - continue the application, leave the debugger
bt (backtrace) - print the execution stack (which functions were called)
n (next) - execute the next command
s (step) - execute the next command, step inside functions
v (var) - display variables (v l prints local, v g prints global variables)
h (help) - list more available commands with details

Moreover it is possible to use standard ruby commands to print or inspect the current objects, using e.g. object.class, objec.inspect methods. And it's also possible to modify the current variables, just use var = value command.

Further links

http://blog.nanorails.com/articles/2006/7/14/a-better-rails-debugger-ruby-debug

Attaching to a Ruby process using GDB

If you need to attach to a running Rails (or in general Ruby) application you can use the GDB debugger. This is needed when your application stops responding, but it is still running and there is no way to start debugger command there.

GDB does not support Ruby debugging directly but there are some macros which make it easy. See Inspecting a live Ruby process page for more information.


Useful Firefox plugins


FireBug plugin

Firebug plugin allows to inspect, debug and edit displayed page. It has integrated CSS and DOM browser. It's a must for a web developer!


Screengrab

Screengrab saves the displayed page as an image (to a file or to the clipboard). Very useful for taking screen shots when reporting layout problems because it can save complete page (even currently not visible parts). It can also save only the visible part or just the user selected part.


Using curl for sending REST API requests


Testing REST API in a web browser is very limited, without an UI (a from) it is possible to make only GET requests. PUT or POST requests cannot be sent.

But we can use curl to test a REST API without web client UI.


Authentication

The main problem is the authentication - REST API requires login before any request.

There are these possibilities:

  1. Put the user name and the password directly to each curl command:
    curl -u <user>:<password> http://localhost:8080/services.xml
  2. Use only the user name option, curl will ask for the password on the terminal:
    curl -u <user> http://localhost:8080/services.xml
    (This is more secure, the password won't be stored in the shell history and it won't be visible on the terminal.)
  3. And probably the best solution: login and save the authorization cookie to a file (cookie.txt in this case):
    curl -u <user> -c cookie.txt http://localhost:8080/login
    Then use the cookie in all following calls:
    curl -b cookie.txt http://localhost:8080/services.xml


GET requests

GET requests are simple, GET is default. See the examples in the previous paragraph.


PUT/POST/DELETE requests

PUT requests can be sent using -X PUT option, optionally with -d option followed by parameters.

Example: curl -d 'execute=start' -b cookie.txt -X PUT http://localhost:8080/services/ntp.xml

This is equivalent to: curl -b cookie.txt -X PUT http://localhost:8080/services/ntp.xml?execute=start


To send a XML input file use -H option (set XML content type) and set the input file using @ prefix in -d option:

Example: curl -b cookie.txt -X PUT -H 'Content-type: text/xml' -d @inputfile.xml http://localhost:8080/system


Parameters can be set also as parameter in json format:

Example: curl -b cookie.txt -X PUT -H 'Content-type: text/x-json' -d "{'routes':{'via':'1.2.3.4', 'id':'default'}}" http://localhost:8080/network/routes/default


For DELETE or POST requests just change the argument in -X option.