aboutsummaryrefslogtreecommitdiffstats
path: root/railties
diff options
context:
space:
mode:
authormiloops <miloops@gmail.com>2008-09-05 20:19:23 -0300
committermiloops <miloops@gmail.com>2008-09-05 20:19:23 -0300
commitc6acd2924e6de8d6c8a6f7e19cc222894a56b903 (patch)
tree54bce2c635d4b3135a1f3f2205103ac4d712a136 /railties
parent6c51c45a0ad66c3b53e3b5b9ea25953e9f1ef90b (diff)
downloadrails-c6acd2924e6de8d6c8a6f7e19cc222894a56b903.tar.gz
rails-c6acd2924e6de8d6c8a6f7e19cc222894a56b903.tar.bz2
rails-c6acd2924e6de8d6c8a6f7e19cc222894a56b903.zip
Added View Helpers to the Rails debugging guide.
Diffstat (limited to 'railties')
-rw-r--r--railties/doc/guides/debugging/debugging_rails_applications.txt99
1 files changed, 99 insertions, 0 deletions
diff --git a/railties/doc/guides/debugging/debugging_rails_applications.txt b/railties/doc/guides/debugging/debugging_rails_applications.txt
index 43b3a61192..2c8477a1e4 100644
--- a/railties/doc/guides/debugging/debugging_rails_applications.txt
+++ b/railties/doc/guides/debugging/debugging_rails_applications.txt
@@ -12,6 +12,105 @@ Unfortunately, you won't find always the answer you are looking for this way. In
If you ever wanted to learn about Rails source code but you didn't know where to start, this may be the best way, just debug any request to your application and use this guide to learn how to move in the code you have written but also go deeper into Rails code.
+== View helpers for debugging
+
+=== debug
+
+*debug* will return a <pre>-tag that has object dumped by YAML. This creates a very readable way to inspect an object.
+
+[source, html]
+----------------------------------------------------------------------------
+<%= debug @post %>
+<p>
+ <b>Title:</b>
+ <%=h @post.title %>
+</p>
+----------------------------------------------------------------------------
+
+Will render something like this:
+
+----------------------------------------------------------------------------
+--- !ruby/object:Post
+attributes:
+ updated_at: 2008-09-05 22:55:47
+ body: It's a very helpful guide for debugging your Rails app.
+ title: Rails debugging guide
+ published: t
+ id: "1"
+ created_at: 2008-09-05 22:55:47
+attributes_cache: {}
+
+
+Title: Rails debugging guide
+----------------------------------------------------------------------------
+
+
+=== local_assigns
+
+If you need to find out whether a certain local variable has been assigned a value in a particular render call, you need to use the following pattern:
+
+[source, html]
+----------------------------------------------------------------------------
+ <% if local_assigns.has_key? :headline %>
+ <p>Headline: <%= headline %></p>
+ <% end %>
+----------------------------------------------------------------------------
+
+Using defined?(headline) will not work. This is an implementation restriction.
+
+[TIP]
+This is particularly handy inside partials, since you can know which variables have been defined and which haven't.
+
+=== do it yourself
+
+Displaying an instance in yaml format, can be achieved this way:
+
+[source, html]
+----------------------------------------------------------------------------
+<%= simple_format @post.to_yaml %>
+<p>
+ <b>Title:</b>
+ <%=h @post.title %>
+</p>
+----------------------------------------------------------------------------
+
+*to_yaml* converts the method to yaml format leaving it more readable and finally *simple_format* help us to render each line as in the console. This is how *debug* method does it's magic.
+
+As a result of this can see something like this in our view:
+
+----------------------------------------------------------------------------
+--- !ruby/object:Post
+attributes:
+updated_at: 2008-09-05 22:55:47
+body: It's a very helpful guide for debugging your Rails app.
+title: Rails debugging guide
+published: t
+id: "1"
+created_at: 2008-09-05 22:55:47
+attributes_cache: {}
+
+Title: Rails debugging guide
+----------------------------------------------------------------------------
+
+Another great method for displaying object values is *inspect*, especially when working with arrays or hashes, it will print the object value as a string, for example:
+
+[source, html]
+----------------------------------------------------------------------------
+<%= [1, 1, 2, 3, 5].inspect %>
+<p>
+ <b>Title:</b>
+ <%=h @post.title %>
+</p>
+----------------------------------------------------------------------------
+
+Will be rendered as follows:
+
+----------------------------------------------------------------------------
+[1, 2, 3]
+
+Title: Rails debugging guide
+----------------------------------------------------------------------------
+
== Debugging with ruby-debug
=== Setup