How do I print out the contents of an object in Rails for easy debugging?
I generally first try .inspect
, if that doesn't give me what I want, I'll switch to .to_yaml
.
class User
attr_accessor :name, :age
end
user = User.new
user.name = "John Smith"
user.age = 30
puts user.inspect
#=> #<User:0x423270c @name="John Smith", @age=30>
puts user.to_yaml
#=> --- !ruby/object:User
#=> age: 30
#=> name: John Smith
Hope that helps.
print and debugging functions in rails?
Short answer is, you can't. At least not in one line. And not only because this is a violation of MVC, there are also practical reasons that prevent this.
There is no reliable way to output a bunch of data in an arbitrary format and keep it valid. Outputting it in JSON views may easily result in invalid data. So if your debug data can only be handled by a browser, that output should only be specified in views for browsers. Even if none other exist, separate concerns.
There is a substitute, of course. Rails 4.2.0 ships an app template with web_console
. All you need to start using it is add a call to console
in your views somewhere, like the app's general layout file. If that's actually ERB, add this line below:
<%= console %>
And wherever it appears, you have a REPL in the context of the currently rendered view, where you can easily inspect objects and even perform actions that change your data.
There is also a variety of methods to output data into the server's console or log file. They've been listed in other answers. I'll add a little to the solution involving logger.
Rails Panel. It's a Chrome extension that adds another tab to Chrome Dev Tools (that show up behind F12) named "Rails". For it to work, you need to add a meta_request
gem to your app (make sure it's in group development
!). Once working, it will show loads of data about how the page was processed:
- Time spent fetching data, rendering it
- Parameters for the given request
- Executed DB queries, duration and lines they've been triggered by
- View files involved
- Log entries emitted on this request and what triggered that
- Errors encountered
This one and some other debugging things are discussed in this Railscast.
On Ruby on Rails, how do we print debug info inside of a controller?
In the controller you can:
render :text => @some_object.inspect
But your view won't be rendered.
You could also:
Rails.logger.debug("My object: #{@some_object.inspect}")
and run tail on log/development.log to see the output.
In the view the recommeneded way is:
<%= debug(@some_object) %>
Printing an array or object in Ruby on rails. Also view the structure of the data
Take a look at this doc, 'Debugging Rails Applications', particularly section 3, 'Debugging with ruby-debug':
http://guides.rubyonrails.org/debugging_rails_applications.html
How do I print the contents of a ruby variable in the view of my Rails Application?
You can use the puts
method from within your views to output information to your server console. You should be able to do the following using Haml from anywhere in your view:
- puts @my_variable.inspect
How do I dump an object's fields to the console?
Possibly:
puts variable.inspect
How do I print the contents of a for loop using (1..10).each?
If you are trying to print this out in Rails (per your comment), try this:
<% (1..10).each do |i| %>
<%= i %>
<% end %>
EDIT: So if you were to want to add these items to a list you would use:
<ol>
<% (1..10).each do |i| %>
<li><%= i %></li>
<% end %>
</ol>
How to make Rails.logger.debug print hash more readable
Nevermind, I found the answer to my own question. I need to use
my_hash = {'a' => 'alligator', 'b'=>'baboon'}
Rails.logger.debug "#{my_hash.inspect}"
Then, it looks like
{"b"=>"baboon", "a"=>"aligator"}
Rails Debug - How to print variable values on every step
You are looking for
display @test_var
After that, @test_var
will be printed every time the debugger stops.
Rails print (dump) variables
I found this: http://www.ruby-doc.org/core-2.1.3/Kernel.html#method-i-p
p variable
is a shortcut for puts variable.inspect
.
Related Topics
How to Generate a Human Readable Time Range Using Ruby on Rails
Why Should One Use a Http Server in Front of a Framework Web Server
How to Generate Zip File Without Saving to the Disk with Ruby
(Ruby) Getting Net::Smtp Working with Gmail...
How to Install JSON Gem - Failed to Build Gem Native Extension(MAC 10.10)
How to Embed Regular Expressions in Other Regular Expressions in Ruby
How to Install Rvm System Requirements Without Giving Sudo Access for Rvm User
Difference Between @Instance_Variable and Attr_Accessor
Ruby Gemspec Dependency: Is Possible Have a Git Branch Dependency
Differencebetween 'After_Create' and 'After_Save' and When to Use Which
How to Deal with the Conflict Between Activesupport::JSON and the JSON Gem
Change the Binding of a Proc in Ruby
Why Doesn't Minitest::Spec Have a Wont_Raise Assertion
What's the Point of Unary Plus Operator in Ruby
Difference Between Has_One and Belongs_To in Rails
How to Get the Name of the Command Called for Usage Prompts in Ruby