Using PUTS to send message to console


#1

Hi,

I’m having trouble with my card not working correctly (not adding to
quantity instead of adding
full new line item) so I figured I would output some messages to the
console to see what clause I
am in, so I tried this:

def add_product(product)
item = @items.find {|i| i.product_id == product.id}
if item
puts “if clause”
item.quantity += 1
else
puts “else clause”
item = LineItem.for_product(product)
@items << item
end
@total_price += product.price
puts “end”
end

but the puts messages are not showing up…is this not a technique that
will work? Is there some
other way I can write to the console?

Hopefully this is my last dumb question of the evening.

Tom


#2

Tom Dell’Aringa wrote:

but the puts messages are not showing up…is this not a technique
that will work? Is there some other way I can write to the console?

When runnign Webrick it should “work”, ie display the output in the
Webrick console window. When running FastCGI I hear rumours it’ll crash
the FastCGI handler. Undefined results outside of those two scenarios, I
guess.

A better way to go about it would probably be to add to your logfile:

logger.info(“if clause”)


#3

— Jakob S. removed_email_address@domain.invalid wrote:

Tom Dell’Aringa wrote:

but the puts messages are not showing up…is this not a technique
that will work? Is there some other way I can write to the console?

When runnign Webrick it should “work”, ie display the output in the
Webrick console window. When running FastCGI I hear rumours it’ll crash
the FastCGI handler. Undefined results outside of those two scenarios, I
guess.

I’m not using FastCGI and am using Webrick.

A better way to go about it would probably be to add to your logfile:

logger.info(“if clause”)

Makes sense, but I got this error when trying it:

undefined local variable or method `logger’ for #<Cart:0x385cd88
@total_price=0.0, @items=[]>

Do I have to declare logger somehow?

Tom


#4

puts never worked for me with webrick. Try: warn ‘blah blah’ …


#5

If you are in a model or controller you access the logger via the
logger variable.
If you are in a view you access the logger via the @logger variable.

http://wiki.rubyonrails.com/rails/pages/HowtoAccessTheLogFile