Forum: Ruby on Rails I need debugging tips?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
C9af522fc39d5de90f21119543c2b9ab?d=identicon&s=25 Steve Litt (Guest)
on 2006-01-06 22:55
(Received via mailing list)
Hi all,

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 Litt
Author:
   * Universal Troubleshooting Process courseware
   * Troubleshooting Techniques of the Successful Technologist
   * Rapid Learning: Secret Weapon of the Successful Technologist
Webmaster
   * Troubleshooters.Com
   * http://www.troubleshooters.com
8acba6d057b3f0c2c16cca95ad36881b?d=identicon&s=25 Bakki Kudva (bakki)
on 2006-01-06 23:07
(Received via mailing list)
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.

-bakki kudva

On 1/6/06, Steve Litt <slitt@troubleshooters.com> wrote:
...
132a94ca65959bda6c74fae54bff2425?d=identicon&s=25 Ezra Zygmuntowicz (Guest)
on 2006-01-06 23:16
(Received via mailing list)
On Jan 6, 2006, at 1:56 PM, Steve Litt wrote:

> I managed to empty the cart by placing session[:cart] = nil in
> and saw an error pointing to code I didn't write (Rails code, in other
>
> Thanks
>
> SteveT

Steve-

	One good way to debug nil errors in your views is to use the debug
method. So if your looping over @items try this at the top of your view:

<%= debig(@items) %>

	You will get a pretty printed yaml version of your object and all
the data it holds. And another way to avoid the nil errors inside of
a loop is to do something like this:

<% @items.each do |item| -%>
    <p><%= item.name rescue nil -%></p>
    <p><%= item.info rescue nil -%></p>
<% end -%>

	The rescue nil deal will just print an empty string instead of
making your whole page error out with a nil error when item.whatever
is nil inside the loop. Hope that helps a bit.

Cheers-
-Ezra Zygmuntowicz
Yakima Herald-Republic
WebMaster
http://yakimaherald.com
509-577-7732
ezra@yakima-herald.com
C9af522fc39d5de90f21119543c2b9ab?d=identicon&s=25 Steve Litt (Guest)
on 2006-01-07 00:13
(Received via mailing list)
On Friday 06 January 2006 05:04 pm, Bakki Kudva 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.
>
> -bakki kudva

Sounds good Bakki,

How do I set a breakpoint?

Thanks

SteveT
3f900b38ec3b2c45427c354722fa4ce3?d=identicon&s=25 Tom Fakes (tomfakes)
on 2006-01-07 00:19
(Received via mailing list)
No-one's mentioned logging yet.

In a controller, you can write this:

	logger.info "My Stuff: #(@items.inspect)"

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.

More info here:
http://wiki.rubyonrails.com/rails/pages/HowtoConfi...
http://wiki.rubyonrails.com/rails/pages/logger
C9af522fc39d5de90f21119543c2b9ab?d=identicon&s=25 Steve Litt (Guest)
on 2006-01-07 00:40
(Received via mailing list)
On Friday 06 January 2006 06:19 pm, Tom Fakes wrote:
> break.
>
> More info here:
> http://wiki.rubyonrails.com/rails/pages/HowtoConfi...
> http://wiki.rubyonrails.com/rails/pages/logger

Oh Oh,

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 }
C9af522fc39d5de90f21119543c2b9ab?d=identicon&s=25 Steve Litt (Guest)
on 2006-01-07 00:46
(Received via mailing list)
On Friday 06 January 2006 05:10 pm, Ezra Zygmuntowicz wrote:

> Steve-
>
> 	One good way to debug nil errors in your views is to use the debug
> method. So if your looping over @items try this at the top of your view:
>
> <%= debig(@items) %>
>
> 	You will get a pretty printed yaml version of your object and all
> the data it holds.

I tried that and didn't see any nils, unless the dash at the bottom,
with
nothing after it, indicates a nil. Does that lone dash indicate a nil?

Thanks

Steve
3f900b38ec3b2c45427c354722fa4ce3?d=identicon&s=25 Tom Fakes (tomfakes)
on 2006-01-07 01:13
(Received via mailing list)
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.
8e44c65ac5b896da534ef2440121c953?d=identicon&s=25 Ezra Zygmuntowicz (Guest)
on 2006-01-07 01:49
(Received via mailing list)
On Jan 6, 2006, at 3:46 PM, Steve Litt wrote:

>> 	You will get a pretty printed yaml version of your object and all
>> the data it holds.
>
> I tried that and didn't see any nils, unless the dash at the
> bottom, with
> nothing after it, indicates a nil. Does that lone dash indicate a nil?
>
> Thanks
>
> Steve


I think it does, but could you paste the debug yaml dump here and I
can tell you?

Cheers-
-Ezra Zygmuntowicz
WebMaster
Yakima Herald-Republic Newspaper
ezra@yakima-herald.com
509-577-7732
8acba6d057b3f0c2c16cca95ad36881b?d=identicon&s=25 Bakki Kudva (bakki)
on 2006-01-07 22:46
(Received via mailing list)
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.

hope it helps,

bakki
6828ffc79486cd2442714bf32286a910?d=identicon&s=25 Vivek Krishna (Guest)
on 2006-01-09 05:14
(Received via mailing list)
How does one single step through the code? I have tried irb and it
doenst
allow me to do that!
This topic is locked and can not be replied to.