Rails puts() not creating any output - Stack Overflow

admin2025-04-21  1

In what I've tried to make the simplest of examples, I don't get any output from a puts() to the Rails console or to the development.log when ranges#ranges is hit. The ranges.html.erb is rendered as expected. What am I not understanding?

class RangesController < ApplicationController
  def ranges
    puts "the_color_hex"
  end

  def set_color_hex
  end
end

From the tail end of the development.log file:

    Started GET "/ranges" for 127.0.0.1 at 2025-01-22 17:31:49 -0500
Processing by RangesController#ranges as HTML
  Rendering layout layouts/application.html.erb
  Rendering ranges/ranges.html.erb within layouts/application
  Rendered ranges/ranges.html.erb within layouts/application (Duration: 1.9ms | GC: 0.5ms)
  Rendered layout layouts/application.html.erb (Duration: 3.7ms | GC: 0.5ms)
Completed 200 OK in 42ms (Views: 5.0ms | ActiveRecord: 0.0ms (0 queries, 0 cached) | GC: 0.5ms)

In what I've tried to make the simplest of examples, I don't get any output from a puts() to the Rails console or to the development.log when ranges#ranges is hit. The ranges.html.erb is rendered as expected. What am I not understanding?

class RangesController < ApplicationController
  def ranges
    puts "the_color_hex"
  end

  def set_color_hex
  end
end

From the tail end of the development.log file:

    Started GET "/ranges" for 127.0.0.1 at 2025-01-22 17:31:49 -0500
Processing by RangesController#ranges as HTML
  Rendering layout layouts/application.html.erb
  Rendering ranges/ranges.html.erb within layouts/application
  Rendered ranges/ranges.html.erb within layouts/application (Duration: 1.9ms | GC: 0.5ms)
  Rendered layout layouts/application.html.erb (Duration: 3.7ms | GC: 0.5ms)
Completed 200 OK in 42ms (Views: 5.0ms | ActiveRecord: 0.0ms (0 queries, 0 cached) | GC: 0.5ms)
Share Improve this question edited Jan 22 at 22:39 Bob Rockefeller asked Jan 22 at 21:38 Bob RockefellerBob Rockefeller 4,6063 gold badges31 silver badges35 bronze badges 7
  • 1 Can you post the actual logs that you do have? – engineersmnky Commented Jan 22 at 21:49
  • Started GET "/ranges" for 127.0.0.1 at 2025-01-22 17:31:49 -0500 Processing by RangesController#ranges as HTML Rendering layout layouts/application.html.erb Rendering ranges/ranges.html.erb within layouts/application Rendered ranges/ranges.html.erb within layouts/application (Duration: 1.9ms | GC: 0.5ms) Rendered layout layouts/application.html.erb (Duration: 3.7ms | GC: 0.5ms) Completed 200 OK in 42ms (Views: 5.0ms | ActiveRecord: 0.0ms (0 queries, 0 cached) | GC: 0.5ms) – Bob Rockefeller Commented Jan 22 at 22:33
  • 1 puts outputs to the terminal, logger.debug outputs to the terminal and log file. – Alex Commented Jan 23 at 5:57
  • Stop using puts for debugging - it's a waste of time and effort. Set a breakpoint instead or just render plain: 'Hello world' if you can't be bothered. github.com/ruby/debug – max Commented Jan 23 at 15:54
  • @dbugger Yes, that's the exact code. – Bob Rockefeller Commented Jan 23 at 16:07
 |  Show 2 more comments

1 Answer 1

Reset to default 0

barring any silly issues like not saving the file or anything -- keep in mind that puts will write to stdout, not your log file. The output should not be inside of development.log (unless you're doing something non-standard).

Typically you would see this output in the terminal where you ran the rails server command.

You could try using logger "hi mom" or Rails.logger "hi mom" to see the message in your development.log file.

转载请注明原文地址:http://anycun.com/QandA/1745227273a90481.html