Thursday, June 14, 2007

Rails - render :action vs render :template

render :action vs render :template

One can use render :action to render the template associated with another action.

However, when the user does

def action1
@foo = "action1"
render :action => "action2"
end

def action2
@foo = "action2"
render
end

the user should realize the method associated with action2 is not exectued when action1 calls render :action => "action2". This is simply the equivalent of render :template => "my_controller/action2".

This can get confusing when action2 in turn uses a non-default template to render itself.

def action1
@foo = "action1"
render :action => "action2"
end

def action2
@foo = "action2"
render :template => "some/other"
end



In this case, when action1 is executed, rails will return an error complaining "views/my_controller/action2.rhtml" was not found.

Wednesday, June 13, 2007

Rails scripts/breakpointer questions

NOTES:
  1. Things of interest : local_variables, instance_variables, caller, methods
  2. Keep a close eye on the prompt. A * means breakpointer (irb really) expects you to enter more ruby code since it senses that the code entered so far is not fully valid yet.
  3. breakpointer (irb really) will by default print the return value to the irb console. You can explicitly return false to disable printing of large datasets.

    breakpointer> @copy = @huge_dataset; false

    For regular irb (and script/console) setting conf.return_format allows you to limit what is printed, but conf object doesn't seem to exist in breakpointer's irb session.

How do you print out variable values?
puts doesn't work :) Update: It actually prints the output along with the script/server output.

You can use @logger.info etc which will print the info to development.log

Local variables
I put a breakpoint in a helper method and tried to view some local variables defined in the helper method, but it didn't work.

breakpoint> local_variables
=> ["id", "block", "_"]

(note that I cleaned the irb prompt and replaced it with breakpoint for blogging)
The reason is breakpoint drops me in the breakpoint.rb's breakpoint method.

breakpoint> caller
=> ["/usr/local/lib/ruby/gems/1.8/gems/rails-1.1.6/lib/breakpoint.rb:512:in `breakpoint'", "/usr/local/lib/ruby/gems/1.8/gems/rails-1.1.6/lib/breakpoint.rb:512:in `breakpoint'", "script/../...


Now to figure out how we pop the stack back up so we end up in the method. Anyone know?

Printing Huge datasets
Suppose I am debugging some calculations I am doing on a hude dataset in breakpointer:

breakpoint> @huge_dataset

This will print the entire dataset. Ctrl-C will stop the display.


breakpoint> @huge_dataset;

This doesn't print the dataset.

So how is one to print the dataset. One solution is to use to_yaml.

breakpoint> fh = File.open("/tmp/foo", "w");
breakpoint> fh.puts(@huge_dataset.to_yaml)
=> nil
breakpoint>

I would really like to print the value in the breakpointer's irb console though.
Is there a special console object that I can target?

Update: Another way to debug your rails app - ruby-debug

Rails script/console questions

How to invoke a method defined in a helper module in script/console?

How to get the list of classes loaded by underlying irb in script/console?

Monday, June 11, 2007

Extending Tcl's package include directories at run-time

A quick one here:
Say you are debugging a tcl package and have a local copy to debug.

To make the tcl script pick up the local copy of the package:

- If the tcl script is editable, append to auto_path as

set auto_path "./src $::auto_path"
package require Foo
Note that you would need to create a pkgIndex.tcl in the directory where your local copy exists.

- If the tcl script is not-editable, use TCLLIBPATH env var. Haven't tried it though.

o&o