Like most other frameworks, Ruby groups code by Model/View/Controller
instead
of by task, and therefore I’m having a hard time debugging a problem
from the
Agile Rails book where looping through @items errors out because one or
more
items is nil.
I managed to empty the cart by placing session[:cart] = nil in
find_cart() in
the store_controller_rb, and the problem continued after I removed that
diagnostic, thereby proving it wasn’t caused just a bad record that got
in
there. I’ve examined the dump screen produced by the error every way but
loose and can’t find anything that helps. I went back and looked at
everything I did recently – nothing obvious. I looked at
log/production.log,
and saw an error pointing to code I didn’t write (Rails code, in other
words).
I’d like to place diagnostic prints in the controller code to see if I’m
placing nil records in the @items whatever, but where would such
diagnostic
prints print out?
So I need some debugging tips. What are your favorite Rails debugging
techniques?
Thanks
SteveT
Steve L.
Author:
Universal Troubleshooting Process courseware
Troubleshooting Techniques of the Successful Technologist
Rapid Learning: Secret Weapon of the Successful Technologist
Webmaster
I like to use breakpoints in the code and invoke script/breakpointer. It
drops me into irb and ALL the environment vars of the controller are
available. You can ctrl-D to continue and if your breakpoint() is in a
loop
you will hit it each time and see the vars changing.
On Friday 06 January 2006 05:04 pm, Bakki K. wrote:
I like to use breakpoints in the code and invoke script/breakpointer. It
drops me into irb and ALL the environment vars of the controller are
available. You can ctrl-D to continue and if your breakpoint() is in a loop
you will hit it each time and see the vars changing.
And this output shows up in the log/development.log file.
The logging functionality is more important to me than the IRB solution,
as
I can leave it in the code and check the log output if I seem something
break.
undefined local variable or method `logger’ for #Cart:0x407f5e2c
This happened before and after I added the following to the bottom of
config/environment.rb:
begin
RAILS_DEFAULT_LOGGER =
Logger.new(“#{RAILS_ROOT}/log/#{RAILS_ENV}.log”)
rescue StandardError
RAILS_DEFAULT_LOGGER = Logger.new(STDERR)
RAILS_DEFAULT_LOGGER.level = Logger::WARN
RAILS_DEFAULT_LOGGER.warn(
"Rails Error: Unable to access log file. Please ensure that
log/#{RAILS_ENV}.log exists and is chmod 0666. " +
“The log level has been raised to WARN and the output directed to
STDERR
until the problem is fixed.”
)
end
[ActiveRecord, ActionController, ActionMailer].each { |mod|
mod::Base.logger
||= RAILS_DEFAULT_LOGGER }
The logger variable is part of your controller. Your original question
was:
“I’d like to place diagnostic prints in the controller code to see if
I’m
placing nil records in the @items whatever, but where would such
diagnostic
prints print out?”
So, this will work:
class MyController < ApplicationController
def some_action
logger.info “My Cart Is: #{@cart.inspect}”
end
end
But your current problem is that you are trying to log from a Model
object
that is not derived from the ActiveRecord::Base class. In that case you
have to do use the RAILS_DEFAULT_LOGGER global constant:
class Cart
def something
RAILS_DEFAULT_LOGGER.info “My Cart Is: #{self.inspect}”
end
end
If you are using Rails 1.0, the code you added to environment.rb is not
necessary, it is built into Rails.
Just insert breakpoint() in your ruby code which you want to examine.
Invoke script/breakpointer and you should see…
No connection to breakpoint service at
druby://localhost:42531(DRb::DRbConnError)
Tries to connect will be made every 2 seconds…
Don’t worry about the ‘No connection at breakpoint’ bit. Carry on with
your
app and when the breakpoint hits you will see something like:
Executing break point at
./script/…/config/…/app/controllers/my_controller.rb:16 in `new’
irb(#MyController:0x4079fef0):001:0>
Which means you are in irb. Now you can simply type in variable names
and
look at their values.
The best documentation I found for the breakpointer is in its source
file
which is at:
/usr/lib/ruby/gems/1.8/gems/activesupport-1.2.5/lib/active_support/breakpoint.rb
on my Debian system.