rails 4.0.0 → 4.2.11.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/README.md +30 -23
- data/guides/CHANGELOG.md +108 -6
- data/guides/Rakefile +21 -6
- data/guides/assets/images/akshaysurve.jpg +0 -0
- data/guides/assets/images/edge_badge.png +0 -0
- data/guides/assets/images/feature_tile.gif +0 -0
- data/guides/assets/images/footer_tile.gif +0 -0
- data/guides/assets/images/fxn.png +0 -0
- data/guides/assets/images/getting_started/article_with_comments.png +0 -0
- data/guides/assets/images/getting_started/challenge.png +0 -0
- data/guides/assets/images/getting_started/confirm_dialog.png +0 -0
- data/guides/assets/images/getting_started/forbidden_attributes_for_new_article.png +0 -0
- data/guides/assets/images/getting_started/form_with_errors.png +0 -0
- data/guides/assets/images/getting_started/index_action_with_edit_link.png +0 -0
- data/guides/assets/images/getting_started/new_article.png +0 -0
- data/guides/assets/images/getting_started/rails_welcome.png +0 -0
- data/guides/assets/images/getting_started/routing_error_no_controller.png +0 -0
- data/guides/assets/images/getting_started/routing_error_no_route_matches.png +0 -0
- data/guides/assets/images/getting_started/show_action_for_articles.png +0 -0
- data/guides/assets/images/getting_started/template_is_missing_articles_new.png +0 -0
- data/guides/assets/images/getting_started/unknown_action_create_for_articles.png +0 -0
- data/guides/assets/images/getting_started/unknown_action_new_for_articles.png +0 -0
- data/guides/assets/images/header_tile.gif +0 -0
- data/guides/assets/images/icons/README +1 -1
- data/guides/assets/images/icons/callouts/11.png +0 -0
- data/guides/assets/images/icons/callouts/12.png +0 -0
- data/guides/assets/images/icons/callouts/13.png +0 -0
- data/guides/assets/images/icons/callouts/15.png +0 -0
- data/guides/assets/images/icons/caution.png +0 -0
- data/guides/assets/images/icons/example.png +0 -0
- data/guides/assets/images/radar.png +0 -0
- data/guides/assets/images/rails4_features.png +0 -0
- data/guides/assets/images/rails_guides_kindle_cover.jpg +0 -0
- data/guides/assets/images/vijaydev.jpg +0 -0
- data/guides/assets/javascripts/guides.js +36 -34
- data/guides/assets/stylesheets/main.css +6 -2
- data/guides/assets/stylesheets/print.css +1 -1
- data/guides/bug_report_templates/action_controller_gem.rb +47 -0
- data/guides/bug_report_templates/action_controller_master.rb +54 -0
- data/guides/bug_report_templates/active_record_gem.rb +5 -2
- data/guides/bug_report_templates/active_record_master.rb +3 -2
- data/guides/bug_report_templates/generic_gem.rb +15 -0
- data/guides/bug_report_templates/generic_master.rb +26 -0
- data/guides/rails_guides.rb +23 -4
- data/guides/rails_guides/generator.rb +1 -1
- data/guides/rails_guides/helpers.rb +4 -2
- data/guides/rails_guides/levenshtein.rb +27 -21
- data/guides/rails_guides/markdown.rb +11 -7
- data/guides/rails_guides/markdown/renderer.rb +1 -1
- data/guides/source/2_2_release_notes.md +3 -3
- data/guides/source/2_3_release_notes.md +12 -12
- data/guides/source/3_0_release_notes.md +10 -13
- data/guides/source/3_1_release_notes.md +7 -4
- data/guides/source/3_2_release_notes.md +17 -14
- data/guides/source/4_0_release_notes.md +110 -54
- data/guides/source/4_1_release_notes.md +730 -0
- data/guides/source/4_2_release_notes.md +877 -0
- data/guides/source/_license.html.erb +1 -1
- data/guides/source/_welcome.html.erb +6 -2
- data/guides/source/action_controller_overview.md +223 -57
- data/guides/source/action_mailer_basics.md +129 -76
- data/guides/source/action_view_overview.md +247 -246
- data/guides/source/active_job_basics.md +339 -0
- data/guides/source/active_model_basics.md +374 -20
- data/guides/source/active_record_basics.md +46 -45
- data/guides/source/active_record_callbacks.md +83 -28
- data/guides/source/{migrations.md → active_record_migrations.md} +191 -275
- data/guides/source/active_record_postgresql.md +433 -0
- data/guides/source/active_record_querying.md +382 -300
- data/guides/source/active_record_validations.md +64 -55
- data/guides/source/active_support_core_extensions.md +229 -187
- data/guides/source/active_support_instrumentation.md +23 -22
- data/guides/source/api_documentation_guidelines.md +167 -15
- data/guides/source/asset_pipeline.md +768 -294
- data/guides/source/association_basics.md +188 -96
- data/guides/source/autoloading_and_reloading_constants.md +1311 -0
- data/guides/source/caching_with_rails.md +45 -11
- data/guides/source/command_line.md +96 -65
- data/guides/source/configuring.md +404 -70
- data/guides/source/contributing_to_ruby_on_rails.md +270 -130
- data/guides/source/credits.html.erb +7 -3
- data/guides/source/debugging_rails_applications.md +471 -284
- data/guides/source/development_dependencies_install.md +115 -21
- data/guides/source/documents.yaml +31 -9
- data/guides/source/engines.md +737 -291
- data/guides/source/form_helpers.md +137 -89
- data/guides/source/generators.md +60 -28
- data/guides/source/getting_started.md +1007 -596
- data/guides/source/i18n.md +178 -96
- data/guides/source/index.html.erb +2 -1
- data/guides/source/initialization.md +248 -104
- data/guides/source/kindle/toc.html.erb +1 -1
- data/guides/source/layout.html.erb +14 -22
- data/guides/source/layouts_and_rendering.md +78 -46
- data/guides/source/maintenance_policy.md +78 -0
- data/guides/source/nested_model_forms.md +10 -7
- data/guides/source/plugins.md +66 -57
- data/guides/source/rails_application_templates.md +49 -12
- data/guides/source/rails_on_rack.md +50 -60
- data/guides/source/routing.md +190 -139
- data/guides/source/ruby_on_rails_guides_guidelines.md +12 -13
- data/guides/source/security.md +134 -83
- data/guides/source/testing.md +322 -200
- data/guides/source/upgrading_ruby_on_rails.md +834 -37
- data/guides/source/working_with_javascript_in_rails.md +36 -26
- data/guides/w3c_validator.rb +2 -0
- metadata +93 -116
- data/guides/assets/images/getting_started/forbidden_attributes_for_new_post.png +0 -0
- data/guides/assets/images/getting_started/new_post.png +0 -0
- data/guides/assets/images/getting_started/post_with_comments.png +0 -0
- data/guides/assets/images/getting_started/show_action_for_posts.png +0 -0
- data/guides/assets/images/getting_started/template_is_missing_posts_new.png +0 -0
- data/guides/assets/images/getting_started/undefined_method_post_path.png +0 -0
- data/guides/assets/images/getting_started/unknown_action_create_for_posts.png +0 -0
- data/guides/assets/images/getting_started/unknown_action_new_for_posts.png +0 -0
- data/guides/assets/images/jaimeiniesta.jpg +0 -0
- data/guides/code/getting_started/Gemfile +0 -43
- data/guides/code/getting_started/Gemfile.lock +0 -150
- data/guides/code/getting_started/README.rdoc +0 -28
- data/guides/code/getting_started/Rakefile +0 -6
- data/guides/code/getting_started/app/assets/javascripts/application.js +0 -16
- data/guides/code/getting_started/app/assets/javascripts/comments.js.coffee +0 -3
- data/guides/code/getting_started/app/assets/javascripts/posts.js.coffee +0 -3
- data/guides/code/getting_started/app/assets/javascripts/welcome.js.coffee +0 -3
- data/guides/code/getting_started/app/assets/stylesheets/application.css +0 -13
- data/guides/code/getting_started/app/assets/stylesheets/comments.css.scss +0 -3
- data/guides/code/getting_started/app/assets/stylesheets/posts.css.scss +0 -3
- data/guides/code/getting_started/app/assets/stylesheets/welcome.css.scss +0 -3
- data/guides/code/getting_started/app/controllers/application_controller.rb +0 -5
- data/guides/code/getting_started/app/controllers/comments_controller.rb +0 -17
- data/guides/code/getting_started/app/controllers/posts_controller.rb +0 -47
- data/guides/code/getting_started/app/controllers/welcome_controller.rb +0 -4
- data/guides/code/getting_started/app/helpers/application_helper.rb +0 -2
- data/guides/code/getting_started/app/helpers/comments_helper.rb +0 -2
- data/guides/code/getting_started/app/helpers/posts_helper.rb +0 -2
- data/guides/code/getting_started/app/helpers/welcome_helper.rb +0 -2
- data/guides/code/getting_started/app/models/comment.rb +0 -3
- data/guides/code/getting_started/app/models/post.rb +0 -7
- data/guides/code/getting_started/app/views/comments/_comment.html.erb +0 -15
- data/guides/code/getting_started/app/views/comments/_form.html.erb +0 -13
- data/guides/code/getting_started/app/views/layouts/application.html.erb +0 -14
- data/guides/code/getting_started/app/views/posts/_form.html.erb +0 -27
- data/guides/code/getting_started/app/views/posts/edit.html.erb +0 -5
- data/guides/code/getting_started/app/views/posts/index.html.erb +0 -21
- data/guides/code/getting_started/app/views/posts/new.html.erb +0 -5
- data/guides/code/getting_started/app/views/posts/show.html.erb +0 -18
- data/guides/code/getting_started/app/views/welcome/index.html.erb +0 -3
- data/guides/code/getting_started/bin/bundle +0 -4
- data/guides/code/getting_started/bin/rails +0 -4
- data/guides/code/getting_started/bin/rake +0 -4
- data/guides/code/getting_started/config.ru +0 -4
- data/guides/code/getting_started/config/application.rb +0 -18
- data/guides/code/getting_started/config/boot.rb +0 -4
- data/guides/code/getting_started/config/database.yml +0 -25
- data/guides/code/getting_started/config/environment.rb +0 -5
- data/guides/code/getting_started/config/environments/development.rb +0 -30
- data/guides/code/getting_started/config/environments/production.rb +0 -80
- data/guides/code/getting_started/config/environments/test.rb +0 -36
- data/guides/code/getting_started/config/initializers/backtrace_silencers.rb +0 -7
- data/guides/code/getting_started/config/initializers/filter_parameter_logging.rb +0 -4
- data/guides/code/getting_started/config/initializers/inflections.rb +0 -16
- data/guides/code/getting_started/config/initializers/locale.rb +0 -9
- data/guides/code/getting_started/config/initializers/mime_types.rb +0 -5
- data/guides/code/getting_started/config/initializers/secret_token.rb +0 -12
- data/guides/code/getting_started/config/initializers/session_store.rb +0 -3
- data/guides/code/getting_started/config/initializers/wrap_parameters.rb +0 -14
- data/guides/code/getting_started/config/locales/en.yml +0 -23
- data/guides/code/getting_started/config/routes.rb +0 -7
- data/guides/code/getting_started/db/migrate/20130122042648_create_posts.rb +0 -10
- data/guides/code/getting_started/db/migrate/20130122045842_create_comments.rb +0 -11
- data/guides/code/getting_started/db/schema.rb +0 -33
- data/guides/code/getting_started/db/seeds.rb +0 -7
- data/guides/code/getting_started/public/404.html +0 -58
- data/guides/code/getting_started/public/422.html +0 -58
- data/guides/code/getting_started/public/500.html +0 -57
- data/guides/code/getting_started/public/favicon.ico +0 -0
- data/guides/code/getting_started/public/robots.txt +0 -5
- data/guides/code/getting_started/test/controllers/comments_controller_test.rb +0 -7
- data/guides/code/getting_started/test/controllers/posts_controller_test.rb +0 -7
- data/guides/code/getting_started/test/controllers/welcome_controller_test.rb +0 -9
- data/guides/code/getting_started/test/fixtures/comments.yml +0 -11
- data/guides/code/getting_started/test/fixtures/posts.yml +0 -9
- data/guides/code/getting_started/test/helpers/comments_helper_test.rb +0 -4
- data/guides/code/getting_started/test/helpers/posts_helper_test.rb +0 -4
- data/guides/code/getting_started/test/helpers/welcome_helper_test.rb +0 -4
- data/guides/code/getting_started/test/models/comment_test.rb +0 -7
- data/guides/code/getting_started/test/models/post_test.rb +0 -7
- data/guides/code/getting_started/test/test_helper.rb +0 -15
- data/guides/source/kindle/KINDLE.md +0 -26
@@ -32,7 +32,7 @@ Ruby on Rails Guides: Credits
|
|
32
32
|
<% end %>
|
33
33
|
|
34
34
|
<%= author('Oscar Del Ben', 'oscardelben', 'oscardelben.jpg') do %>
|
35
|
-
Oscar Del Ben is a software engineer at <a href="http://www.wildfireapp.com/">Wildfire</a>. He's a regular open source contributor (<a href="https://github.com/oscardelben">
|
35
|
+
Oscar Del Ben is a software engineer at <a href="http://www.wildfireapp.com/">Wildfire</a>. He's a regular open source contributor (<a href="https://github.com/oscardelben">GitHub account</a>) and tweets regularly at <a href="https://twitter.com/oscardelben">@oscardelben</a>.
|
36
36
|
<% end %>
|
37
37
|
|
38
38
|
<%= author('Frederick Cheung', 'fcheung') do %>
|
@@ -40,7 +40,7 @@ Oscar Del Ben is a software engineer at <a href="http://www.wildfireapp.com/">Wi
|
|
40
40
|
<% end %>
|
41
41
|
|
42
42
|
<%= author('Tore Darell', 'toretore') do %>
|
43
|
-
Tore Darell is an independent developer based in Menton, France who specialises in cruft-free web applications using Ruby, Rails and unobtrusive JavaScript.
|
43
|
+
Tore Darell is an independent developer based in Menton, France who specialises in cruft-free web applications using Ruby, Rails and unobtrusive JavaScript. You can follow him on <a href="http://twitter.com/toretore">Twitter</a>.
|
44
44
|
<% end %>
|
45
45
|
|
46
46
|
<%= author('Jeff Dean', 'zilkey') do %>
|
@@ -64,7 +64,7 @@ Oscar Del Ben is a software engineer at <a href="http://www.wildfireapp.com/">Wi
|
|
64
64
|
<% end %>
|
65
65
|
|
66
66
|
<%= author('Pratik Naik', 'lifo') do %>
|
67
|
-
Pratik Naik is a Ruby on Rails developer at <a href="
|
67
|
+
Pratik Naik is a Ruby on Rails developer at <a href="https://basecamp.com/">Basecamp</a> and also a member of the <a href="http://rubyonrails.org/core">Rails core team</a>. He maintains a blog at <a href="http://m.onkey.org">has_many :bugs, :through => :rails</a> and has a semi-active <a href="http://twitter.com/lifo">twitter account</a>.
|
68
68
|
<% end %>
|
69
69
|
|
70
70
|
<%= author('Emilio Tagua', 'miloops') do %>
|
@@ -74,3 +74,7 @@ Oscar Del Ben is a software engineer at <a href="http://www.wildfireapp.com/">Wi
|
|
74
74
|
<%= author('Heiko Webers', 'hawe') do %>
|
75
75
|
Heiko Webers is the founder of <a href="http://www.bauland42.de">bauland42</a>, a German web application security consulting and development company focused on Ruby on Rails. He blogs at the <a href="http://www.rorsecurity.info">Ruby on Rails Security Project</a>. After 10 years of desktop application development, Heiko has rarely looked back.
|
76
76
|
<% end %>
|
77
|
+
|
78
|
+
<%= author('Akshay Surve', 'startupjockey', 'akshaysurve.jpg') do %>
|
79
|
+
Akshay Surve is the Founder at <a href="http://www.deltax.com">DeltaX</a>, hackathon specialist, a midnight code junkie and occasionally writes prose. You can connect with him on <a href="https://twitter.com/akshaysurve">Twitter</a>, <a href="http://www.linkedin.com/in/akshaysurve">Linkedin</a>, <a href="http://www.akshaysurve.com/">Personal Blog</a> or <a href="http://www.quora.com/Akshay-Surve">Quora</a>.
|
80
|
+
<% end %>
|
@@ -26,17 +26,17 @@ One common task is to inspect the contents of a variable. In Rails, you can do t
|
|
26
26
|
The `debug` helper will return a \<pre> tag that renders the object using the YAML format. This will generate human-readable data from any object. For example, if you have this code in a view:
|
27
27
|
|
28
28
|
```html+erb
|
29
|
-
<%= debug @
|
29
|
+
<%= debug @article %>
|
30
30
|
<p>
|
31
31
|
<b>Title:</b>
|
32
|
-
<%= @
|
32
|
+
<%= @article.title %>
|
33
33
|
</p>
|
34
34
|
```
|
35
35
|
|
36
36
|
You'll see something like this:
|
37
37
|
|
38
38
|
```yaml
|
39
|
-
--- !ruby/object
|
39
|
+
--- !ruby/object Article
|
40
40
|
attributes:
|
41
41
|
updated_at: 2008-09-05 22:55:47
|
42
42
|
body: It's a very helpful guide for debugging your Rails app.
|
@@ -55,10 +55,10 @@ Title: Rails debugging guide
|
|
55
55
|
Displaying an instance variable, or any other object or method, in YAML format can be achieved this way:
|
56
56
|
|
57
57
|
```html+erb
|
58
|
-
<%= simple_format @
|
58
|
+
<%= simple_format @article.to_yaml %>
|
59
59
|
<p>
|
60
60
|
<b>Title:</b>
|
61
|
-
<%= @
|
61
|
+
<%= @article.title %>
|
62
62
|
</p>
|
63
63
|
```
|
64
64
|
|
@@ -67,7 +67,7 @@ The `to_yaml` method converts the method to YAML format leaving it more readable
|
|
67
67
|
As a result of this, you will have something like this in your view:
|
68
68
|
|
69
69
|
```yaml
|
70
|
-
--- !ruby/object
|
70
|
+
--- !ruby/object Article
|
71
71
|
attributes:
|
72
72
|
updated_at: 2008-09-05 22:55:47
|
73
73
|
body: It's a very helpful guide for debugging your Rails app.
|
@@ -88,7 +88,7 @@ Another useful method for displaying object values is `inspect`, especially when
|
|
88
88
|
<%= [1, 2, 3, 4, 5].inspect %>
|
89
89
|
<p>
|
90
90
|
<b>Title:</b>
|
91
|
-
<%= @
|
91
|
+
<%= @article.title %>
|
92
92
|
</p>
|
93
93
|
```
|
94
94
|
|
@@ -123,7 +123,7 @@ config.logger = Logger.new(STDOUT)
|
|
123
123
|
config.logger = Log4r::Logger.new("Application Log")
|
124
124
|
```
|
125
125
|
|
126
|
-
TIP: By default, each log is created under `Rails.root/log/` and the log file
|
126
|
+
TIP: By default, each log is created under `Rails.root/log/` and the log file is named after the environment in which the application is running.
|
127
127
|
|
128
128
|
### Log Levels
|
129
129
|
|
@@ -138,7 +138,7 @@ Rails.logger.level = 0 # at any time
|
|
138
138
|
|
139
139
|
This is useful when you want to log under development or staging, but you don't want to flood your production log with unnecessary information.
|
140
140
|
|
141
|
-
TIP: The default Rails log level is `
|
141
|
+
TIP: The default Rails log level is `debug` in all environments.
|
142
142
|
|
143
143
|
### Sending Messages
|
144
144
|
|
@@ -153,18 +153,18 @@ logger.fatal "Terminating application, raised unrecoverable error!!!"
|
|
153
153
|
Here's an example of a method instrumented with extra logging:
|
154
154
|
|
155
155
|
```ruby
|
156
|
-
class
|
156
|
+
class ArticlesController < ApplicationController
|
157
157
|
# ...
|
158
158
|
|
159
159
|
def create
|
160
|
-
@
|
161
|
-
logger.debug "New
|
162
|
-
logger.debug "
|
163
|
-
|
164
|
-
if @
|
165
|
-
flash[:notice] =
|
166
|
-
logger.debug "The
|
167
|
-
redirect_to(@
|
160
|
+
@article = Article.new(params[:article])
|
161
|
+
logger.debug "New article: #{@article.attributes.inspect}"
|
162
|
+
logger.debug "Article should be valid: #{@article.valid?}"
|
163
|
+
|
164
|
+
if @article.save
|
165
|
+
flash[:notice] = 'Article was successfully created.'
|
166
|
+
logger.debug "The article was saved and now the user is going to be redirected..."
|
167
|
+
redirect_to(@article)
|
168
168
|
else
|
169
169
|
render action: "new"
|
170
170
|
end
|
@@ -177,28 +177,28 @@ end
|
|
177
177
|
Here's an example of the log generated when this controller action is executed:
|
178
178
|
|
179
179
|
```
|
180
|
-
Processing
|
180
|
+
Processing ArticlesController#create (for 127.0.0.1 at 2008-09-08 11:52:54) [POST]
|
181
181
|
Session ID: BAh7BzoMY3NyZl9pZCIlMDY5MWU1M2I1ZDRjODBlMzkyMWI1OTg2NWQyNzViZjYiCmZsYXNoSUM6J0FjdGl
|
182
182
|
vbkNvbnRyb2xsZXI6OkZsYXNoOjpGbGFzaEhhc2h7AAY6CkB1c2VkewA=--b18cd92fba90eacf8137e5f6b3b06c4d724596a4
|
183
|
-
Parameters: {"commit"=>"Create", "
|
183
|
+
Parameters: {"commit"=>"Create", "article"=>{"title"=>"Debugging Rails",
|
184
184
|
"body"=>"I'm learning how to print in logs!!!", "published"=>"0"},
|
185
|
-
"authenticity_token"=>"2059c1286e93402e389127b1153204e0d1e275dd", "action"=>"create", "controller"=>"
|
186
|
-
New
|
185
|
+
"authenticity_token"=>"2059c1286e93402e389127b1153204e0d1e275dd", "action"=>"create", "controller"=>"articles"}
|
186
|
+
New article: {"updated_at"=>nil, "title"=>"Debugging Rails", "body"=>"I'm learning how to print in logs!!!",
|
187
187
|
"published"=>false, "created_at"=>nil}
|
188
|
-
|
189
|
-
|
188
|
+
Article should be valid: true
|
189
|
+
Article Create (0.000443) INSERT INTO "articles" ("updated_at", "title", "body", "published",
|
190
190
|
"created_at") VALUES('2008-09-08 14:52:54', 'Debugging Rails',
|
191
191
|
'I''m learning how to print in logs!!!', 'f', '2008-09-08 14:52:54')
|
192
|
-
The
|
193
|
-
Redirected to
|
194
|
-
Completed in 0.01224 (81 reqs/sec) | DB: 0.00044 (3%) | 302 Found [http://localhost/
|
192
|
+
The article was saved and now the user is going to be redirected...
|
193
|
+
Redirected to # Article:0x20af760>
|
194
|
+
Completed in 0.01224 (81 reqs/sec) | DB: 0.00044 (3%) | 302 Found [http://localhost/articles]
|
195
195
|
```
|
196
196
|
|
197
197
|
Adding extra logging like this makes it easy to search for unexpected or unusual behavior in your logs. If you add extra logging, be sure to make sensible use of log levels to avoid filling your production logs with useless trivia.
|
198
198
|
|
199
199
|
### Tagged Logging
|
200
200
|
|
201
|
-
When running multi-user, multi-account applications, it
|
201
|
+
When running multi-user, multi-account applications, it's often useful
|
202
202
|
to be able to filter the logs using some custom rules. `TaggedLogging`
|
203
203
|
in Active Support helps in doing exactly that by stamping log lines with subdomains, request ids, and anything else to aid debugging such applications.
|
204
204
|
|
@@ -209,430 +209,591 @@ logger.tagged("BCX", "Jason") { logger.info "Stuff" } # Logs "
|
|
209
209
|
logger.tagged("BCX") { logger.tagged("Jason") { logger.info "Stuff" } } # Logs "[BCX] [Jason] Stuff"
|
210
210
|
```
|
211
211
|
|
212
|
-
|
212
|
+
### Impact of Logs on Performance
|
213
|
+
Logging will always have a small impact on performance of your rails app,
|
214
|
+
particularly when logging to disk. However, there are a few subtleties:
|
215
|
+
|
216
|
+
Using the `:debug` level will have a greater performance penalty than `:fatal`,
|
217
|
+
as a far greater number of strings are being evaluated and written to the
|
218
|
+
log output (e.g. disk).
|
219
|
+
|
220
|
+
Another potential pitfall is that if you have many calls to `Logger` like this
|
221
|
+
in your code:
|
222
|
+
|
223
|
+
```ruby
|
224
|
+
logger.debug "Person attributes hash: #{@person.attributes.inspect}"
|
225
|
+
```
|
226
|
+
|
227
|
+
In the above example, There will be a performance impact even if the allowed
|
228
|
+
output level doesn't include debug. The reason is that Ruby has to evaluate
|
229
|
+
these strings, which includes instantiating the somewhat heavy `String` object
|
230
|
+
and interpolating the variables, and which takes time.
|
231
|
+
Therefore, it's recommended to pass blocks to the logger methods, as these are
|
232
|
+
only evaluated if the output level is the same or included in the allowed level
|
233
|
+
(i.e. lazy loading). The same code rewritten would be:
|
234
|
+
|
235
|
+
```ruby
|
236
|
+
logger.debug {"Person attributes hash: #{@person.attributes.inspect}"}
|
237
|
+
```
|
238
|
+
|
239
|
+
The contents of the block, and therefore the string interpolation, is only
|
240
|
+
evaluated if debug is enabled. This performance savings is only really
|
241
|
+
noticeable with large amounts of logging, but it's a good practice to employ.
|
242
|
+
|
243
|
+
Debugging with the `byebug` gem
|
213
244
|
---------------------------------
|
214
245
|
|
215
|
-
When your code is behaving in unexpected ways, you can try printing to logs or
|
246
|
+
When your code is behaving in unexpected ways, you can try printing to logs or
|
247
|
+
the console to diagnose the problem. Unfortunately, there are times when this
|
248
|
+
sort of error tracking is not effective in finding the root cause of a problem.
|
249
|
+
When you actually need to journey into your running source code, the debugger
|
250
|
+
is your best companion.
|
216
251
|
|
217
|
-
The debugger can also help you if you want to learn about the Rails source code
|
252
|
+
The debugger can also help you if you want to learn about the Rails source code
|
253
|
+
but don't know where to start. Just debug any request to your application and
|
254
|
+
use this guide to learn how to move from the code you have written deeper into
|
255
|
+
Rails code.
|
218
256
|
|
219
257
|
### Setup
|
220
258
|
|
221
|
-
You can use the `
|
259
|
+
You can use the `byebug` gem to set breakpoints and step through live code in
|
260
|
+
Rails. To install it, just run:
|
222
261
|
|
223
262
|
```bash
|
224
|
-
$ gem install
|
263
|
+
$ gem install byebug
|
225
264
|
```
|
226
265
|
|
227
|
-
|
266
|
+
Inside any Rails application you can then invoke the debugger by calling the
|
267
|
+
`byebug` method.
|
228
268
|
|
229
269
|
Here's an example:
|
230
270
|
|
231
271
|
```ruby
|
232
272
|
class PeopleController < ApplicationController
|
233
273
|
def new
|
234
|
-
|
274
|
+
byebug
|
235
275
|
@person = Person.new
|
236
276
|
end
|
237
277
|
end
|
238
278
|
```
|
239
279
|
|
240
|
-
|
280
|
+
### The Shell
|
281
|
+
|
282
|
+
As soon as your application calls the `byebug` method, the debugger will be
|
283
|
+
started in a debugger shell inside the terminal window where you launched your
|
284
|
+
application server, and you will be placed at the debugger's prompt `(byebug)`.
|
285
|
+
Before the prompt, the code around the line that is about to be run will be
|
286
|
+
displayed and the current line will be marked by '=>'. Like this:
|
241
287
|
|
242
288
|
```
|
243
|
-
|
289
|
+
[1, 10] in /PathTo/project/app/controllers/articles_controller.rb
|
290
|
+
3:
|
291
|
+
4: # GET /articles
|
292
|
+
5: # GET /articles.json
|
293
|
+
6: def index
|
294
|
+
7: byebug
|
295
|
+
=> 8: @articles = Article.find_recent
|
296
|
+
9:
|
297
|
+
10: respond_to do |format|
|
298
|
+
11: format.html # index.html.erb
|
299
|
+
12: format.json { render json: @articles }
|
300
|
+
|
301
|
+
(byebug)
|
244
302
|
```
|
245
303
|
|
246
|
-
|
304
|
+
If you got there by a browser request, the browser tab containing the request
|
305
|
+
will be hung until the debugger has finished and the trace has finished
|
306
|
+
processing the entire request.
|
307
|
+
|
308
|
+
For example:
|
247
309
|
|
248
310
|
```bash
|
249
|
-
$ rails server --debugger
|
250
311
|
=> Booting WEBrick
|
251
|
-
=> Rails
|
252
|
-
=>
|
253
|
-
|
254
|
-
|
312
|
+
=> Rails 4.2.0 application starting in development on http://0.0.0.0:3000
|
313
|
+
=> Run `rails server -h` for more startup options
|
314
|
+
=> Notice: server is listening on all interfaces (0.0.0.0). Consider using 127.0.0.1 (--binding option)
|
315
|
+
=> Ctrl-C to shutdown server
|
316
|
+
[2014-04-11 13:11:47] INFO WEBrick 1.3.1
|
317
|
+
[2014-04-11 13:11:47] INFO ruby 2.1.1 (2014-02-24) [i686-linux]
|
318
|
+
[2014-04-11 13:11:47] INFO WEBrick::HTTPServer#start: pid=6370 port=3000
|
255
319
|
|
256
|
-
TIP: In development mode, you can dynamically `require \'debugger\'` instead of restarting the server, even if it was started without `--debugger`.
|
257
320
|
|
258
|
-
|
321
|
+
Started GET "/" for 127.0.0.1 at 2014-04-11 13:11:48 +0200
|
322
|
+
ActiveRecord::SchemaMigration Load (0.2ms) SELECT "schema_migrations".* FROM "schema_migrations"
|
323
|
+
Processing by ArticlesController#index as HTML
|
259
324
|
|
260
|
-
|
325
|
+
[3, 12] in /PathTo/project/app/controllers/articles_controller.rb
|
326
|
+
3:
|
327
|
+
4: # GET /articles
|
328
|
+
5: # GET /articles.json
|
329
|
+
6: def index
|
330
|
+
7: byebug
|
331
|
+
=> 8: @articles = Article.find_recent
|
332
|
+
9:
|
333
|
+
10: respond_to do |format|
|
334
|
+
11: format.html # index.html.erb
|
335
|
+
12: format.json { render json: @articles }
|
261
336
|
|
262
|
-
|
337
|
+
(byebug)
|
338
|
+
```
|
263
339
|
|
264
|
-
|
340
|
+
Now it's time to explore and dig into your application. A good place to start is
|
341
|
+
by asking the debugger for help. Type: `help`
|
265
342
|
|
266
|
-
```bash
|
267
|
-
@posts = Post.all
|
268
|
-
(rdb:7)
|
269
343
|
```
|
344
|
+
(byebug) help
|
270
345
|
|
271
|
-
|
346
|
+
byebug 2.7.0
|
272
347
|
|
273
|
-
```
|
274
|
-
(rdb:7) help
|
275
|
-
ruby-debug help v0.10.2
|
276
348
|
Type 'help <command-name>' for help on a specific command
|
277
349
|
|
278
350
|
Available commands:
|
279
|
-
backtrace delete enable help next
|
280
|
-
break disable eval info
|
281
|
-
catch display exit
|
282
|
-
condition down finish
|
283
|
-
continue edit frame
|
351
|
+
backtrace delete enable help list pry next restart source up
|
352
|
+
break disable eval info method ps save step var
|
353
|
+
catch display exit interrupt next putl set thread
|
354
|
+
condition down finish irb p quit show trace
|
355
|
+
continue edit frame kill pp reload skip undisplay
|
284
356
|
```
|
285
357
|
|
286
|
-
TIP: To view the help menu for any command use `help <command-name>` at the
|
358
|
+
TIP: To view the help menu for any command use `help <command-name>` at the
|
359
|
+
debugger prompt. For example: _`help list`_. You can abbreviate any debugging
|
360
|
+
command by supplying just enough letters to distinguish them from other
|
361
|
+
commands, so you can also use `l` for the `list` command, for example.
|
287
362
|
|
288
|
-
|
363
|
+
To see the previous ten lines you should type `list-` (or `l-`)
|
289
364
|
|
290
|
-
This command shows you where you are in the code by printing 10 lines centered around the current line; the current line in this particular case is line 6 and is marked by `=>`.
|
291
|
-
|
292
|
-
```
|
293
|
-
(rdb:7) list
|
294
|
-
[1, 10] in /PathTo/project/app/controllers/posts_controller.rb
|
295
|
-
1 class PostsController < ApplicationController
|
296
|
-
2 # GET /posts
|
297
|
-
3 # GET /posts.json
|
298
|
-
4 def index
|
299
|
-
5 debugger
|
300
|
-
=> 6 @posts = Post.all
|
301
|
-
7
|
302
|
-
8 respond_to do |format|
|
303
|
-
9 format.html # index.html.erb
|
304
|
-
10 format.json { render :json => @posts }
|
305
365
|
```
|
366
|
+
(byebug) l-
|
306
367
|
|
307
|
-
|
368
|
+
[1, 10] in /PathTo/project/app/controllers/articles_controller.rb
|
369
|
+
1 class ArticlesController < ApplicationController
|
370
|
+
2 before_action :set_article, only: [:show, :edit, :update, :destroy]
|
371
|
+
3
|
372
|
+
4 # GET /articles
|
373
|
+
5 # GET /articles.json
|
374
|
+
6 def index
|
375
|
+
7 byebug
|
376
|
+
8 @articles = Article.find_recent
|
377
|
+
9
|
378
|
+
10 respond_to do |format|
|
308
379
|
|
309
380
|
```
|
310
|
-
(rdb:7) l
|
311
|
-
[11, 20] in /PathTo/project/app/controllers/posts_controller.rb
|
312
|
-
11 end
|
313
|
-
12 end
|
314
|
-
13
|
315
|
-
14 # GET /posts/1
|
316
|
-
15 # GET /posts/1.json
|
317
|
-
16 def show
|
318
|
-
17 @post = Post.find(params[:id])
|
319
|
-
18
|
320
|
-
19 respond_to do |format|
|
321
|
-
20 format.html # show.html.erb
|
322
|
-
```
|
323
|
-
|
324
|
-
And so on until the end of the current file. When the end of file is reached, the `list` command will start again from the beginning of the file and continue again up to the end, treating the file as a circular buffer.
|
325
381
|
|
326
|
-
|
382
|
+
This way you can move inside the file, being able to see the code above and over
|
383
|
+
the line where you added the `byebug` call. Finally, to see where you are in
|
384
|
+
the code again you can type `list=`
|
327
385
|
|
328
386
|
```
|
329
|
-
(
|
330
|
-
[1, 10] in /PathTo/project/app/controllers/posts_controller.rb
|
331
|
-
1 class PostsController < ApplicationController
|
332
|
-
2 # GET /posts
|
333
|
-
3 # GET /posts.json
|
334
|
-
4 def index
|
335
|
-
5 debugger
|
336
|
-
6 @posts = Post.all
|
337
|
-
7
|
338
|
-
8 respond_to do |format|
|
339
|
-
9 format.html # index.html.erb
|
340
|
-
10 format.json { render :json => @posts }
|
341
|
-
```
|
387
|
+
(byebug) list=
|
342
388
|
|
343
|
-
|
344
|
-
|
389
|
+
[3, 12] in /PathTo/project/app/controllers/articles_controller.rb
|
390
|
+
3:
|
391
|
+
4: # GET /articles
|
392
|
+
5: # GET /articles.json
|
393
|
+
6: def index
|
394
|
+
7: byebug
|
395
|
+
=> 8: @articles = Article.find_recent
|
396
|
+
9:
|
397
|
+
10: respond_to do |format|
|
398
|
+
11: format.html # index.html.erb
|
399
|
+
12: format.json { render json: @articles }
|
345
400
|
|
346
|
-
|
347
|
-
(rdb:7) list=
|
348
|
-
[1, 10] in /PathTo/project/app/controllers/posts_controller.rb
|
349
|
-
1 class PostsController < ApplicationController
|
350
|
-
2 # GET /posts
|
351
|
-
3 # GET /posts.json
|
352
|
-
4 def index
|
353
|
-
5 debugger
|
354
|
-
=> 6 @posts = Post.all
|
355
|
-
7
|
356
|
-
8 respond_to do |format|
|
357
|
-
9 format.html # index.html.erb
|
358
|
-
10 format.json { render :json => @posts }
|
401
|
+
(byebug)
|
359
402
|
```
|
360
403
|
|
361
404
|
### The Context
|
362
405
|
|
363
|
-
When you start debugging your application, you will be placed in different
|
364
|
-
|
365
|
-
|
366
|
-
|
367
|
-
|
368
|
-
|
369
|
-
|
370
|
-
|
371
|
-
|
372
|
-
|
373
|
-
|
374
|
-
|
375
|
-
|
376
|
-
|
377
|
-
|
378
|
-
|
406
|
+
When you start debugging your application, you will be placed in different
|
407
|
+
contexts as you go through the different parts of the stack.
|
408
|
+
|
409
|
+
The debugger creates a context when a stopping point or an event is reached. The
|
410
|
+
context has information about the suspended program which enables the debugger
|
411
|
+
to inspect the frame stack, evaluate variables from the perspective of the
|
412
|
+
debugged program, and contains information about the place where the debugged
|
413
|
+
program is stopped.
|
414
|
+
|
415
|
+
At any time you can call the `backtrace` command (or its alias `where`) to print
|
416
|
+
the backtrace of the application. This can be very helpful to know how you got
|
417
|
+
where you are. If you ever wondered about how you got somewhere in your code,
|
418
|
+
then `backtrace` will supply the answer.
|
419
|
+
|
420
|
+
```
|
421
|
+
(byebug) where
|
422
|
+
--> #0 ArticlesController.index
|
423
|
+
at /PathTo/project/test_app/app/controllers/articles_controller.rb:8
|
424
|
+
#1 ActionController::ImplicitRender.send_action(method#String, *args#Array)
|
425
|
+
at /PathToGems/actionpack-4.2.0/lib/action_controller/metal/implicit_render.rb:4
|
426
|
+
#2 AbstractController::Base.process_action(action#NilClass, *args#Array)
|
427
|
+
at /PathToGems/actionpack-4.2.0/lib/abstract_controller/base.rb:189
|
428
|
+
#3 ActionController::Rendering.process_action(action#NilClass, *args#NilClass)
|
429
|
+
at /PathToGems/actionpack-4.2.0/lib/action_controller/metal/rendering.rb:10
|
379
430
|
...
|
380
431
|
```
|
381
432
|
|
382
|
-
|
433
|
+
The current frame is marked with `-->`. You can move anywhere you want in this
|
434
|
+
trace (thus changing the context) by using the `frame _n_` command, where _n_ is
|
435
|
+
the specified frame number. If you do that, `byebug` will display your new
|
436
|
+
context.
|
383
437
|
|
384
438
|
```
|
385
|
-
(
|
386
|
-
|
387
|
-
|
439
|
+
(byebug) frame 2
|
440
|
+
|
441
|
+
[184, 193] in /PathToGems/actionpack-4.2.0/lib/abstract_controller/base.rb
|
442
|
+
184: # is the intended way to override action dispatching.
|
443
|
+
185: #
|
444
|
+
186: # Notice that the first argument is the method to be dispatched
|
445
|
+
187: # which is *not* necessarily the same as the action name.
|
446
|
+
188: def process_action(method_name, *args)
|
447
|
+
=> 189: send_action(method_name, *args)
|
448
|
+
190: end
|
449
|
+
191:
|
450
|
+
192: # Actually call the method associated with the action. Override
|
451
|
+
193: # this method if you wish to change how action methods are called,
|
452
|
+
|
453
|
+
(byebug)
|
388
454
|
```
|
389
455
|
|
390
|
-
The available variables are the same as if you were running the code line by
|
456
|
+
The available variables are the same as if you were running the code line by
|
457
|
+
line. After all, that's what debugging is.
|
391
458
|
|
392
|
-
|
459
|
+
You can also use `up [n]` (`u` for abbreviated) and `down [n]` commands in order
|
460
|
+
to change the context _n_ frames up or down the stack respectively. _n_ defaults
|
461
|
+
to one. Up in this case is towards higher-numbered stack frames, and down is
|
462
|
+
towards lower-numbered stack frames.
|
393
463
|
|
394
464
|
### Threads
|
395
465
|
|
396
|
-
The debugger can list, stop, resume and switch between running threads by using
|
466
|
+
The debugger can list, stop, resume and switch between running threads by using
|
467
|
+
the `thread` command (or the abbreviated `th`). This command has a handful of
|
468
|
+
options:
|
397
469
|
|
398
470
|
* `thread` shows the current thread.
|
399
|
-
* `thread list` is used to list all threads and their statuses. The plus +
|
471
|
+
* `thread list` is used to list all threads and their statuses. The plus +
|
472
|
+
character and the number indicates the current thread of execution.
|
400
473
|
* `thread stop _n_` stop thread _n_.
|
401
474
|
* `thread resume _n_` resumes thread _n_.
|
402
475
|
* `thread switch _n_` switches the current thread context to _n_.
|
403
476
|
|
404
|
-
This command is very helpful, among other occasions, when you are debugging
|
477
|
+
This command is very helpful, among other occasions, when you are debugging
|
478
|
+
concurrent threads and need to verify that there are no race conditions in your
|
479
|
+
code.
|
405
480
|
|
406
481
|
### Inspecting Variables
|
407
482
|
|
408
|
-
Any expression can be evaluated in the current context. To evaluate an
|
409
|
-
|
410
|
-
|
411
|
-
|
412
|
-
|
413
|
-
|
414
|
-
|
415
|
-
[
|
416
|
-
|
417
|
-
|
418
|
-
|
419
|
-
|
420
|
-
|
421
|
-
|
422
|
-
|
423
|
-
|
424
|
-
|
425
|
-
|
426
|
-
|
483
|
+
Any expression can be evaluated in the current context. To evaluate an
|
484
|
+
expression, just type it!
|
485
|
+
|
486
|
+
This example shows how you can print the instance variables defined within the
|
487
|
+
current context:
|
488
|
+
|
489
|
+
```
|
490
|
+
[3, 12] in /PathTo/project/app/controllers/articles_controller.rb
|
491
|
+
3:
|
492
|
+
4: # GET /articles
|
493
|
+
5: # GET /articles.json
|
494
|
+
6: def index
|
495
|
+
7: byebug
|
496
|
+
=> 8: @articles = Article.find_recent
|
497
|
+
9:
|
498
|
+
10: respond_to do |format|
|
499
|
+
11: format.html # index.html.erb
|
500
|
+
12: format.json { render json: @articles }
|
501
|
+
|
502
|
+
(byebug) instance_variables
|
503
|
+
[:@_action_has_layout, :@_routes, :@_headers, :@_status, :@_request,
|
504
|
+
:@_response, :@_env, :@_prefixes, :@_lookup_context, :@_action_name,
|
505
|
+
:@_response_body, :@marked_for_same_origin_verification, :@_config]
|
506
|
+
```
|
507
|
+
|
508
|
+
As you may have figured out, all of the variables that you can access from a
|
509
|
+
controller are displayed. This list is dynamically updated as you execute code.
|
510
|
+
For example, run the next line using `next` (you'll learn more about this
|
511
|
+
command later in this guide).
|
512
|
+
|
513
|
+
```
|
514
|
+
(byebug) next
|
515
|
+
[5, 14] in /PathTo/project/app/controllers/articles_controller.rb
|
516
|
+
5 # GET /articles.json
|
517
|
+
6 def index
|
518
|
+
7 byebug
|
519
|
+
8 @articles = Article.find_recent
|
520
|
+
9
|
521
|
+
=> 10 respond_to do |format|
|
522
|
+
11 format.html # index.html.erb
|
523
|
+
12 format.json { render json: @articles }
|
524
|
+
13 end
|
525
|
+
14 end
|
526
|
+
15
|
527
|
+
(byebug)
|
427
528
|
```
|
428
529
|
|
429
530
|
And then ask again for the instance_variables:
|
430
531
|
|
431
532
|
```
|
432
|
-
(
|
533
|
+
(byebug) instance_variables.include? "@articles"
|
433
534
|
true
|
434
535
|
```
|
435
536
|
|
436
|
-
Now `@
|
537
|
+
Now `@articles` is included in the instance variables, because the line defining it
|
538
|
+
was executed.
|
437
539
|
|
438
|
-
TIP: You can also step into **irb** mode with the command `irb` (of course!).
|
540
|
+
TIP: You can also step into **irb** mode with the command `irb` (of course!).
|
541
|
+
This way an irb session will be started within the context you invoked it. But
|
542
|
+
be warned: this is an experimental feature.
|
439
543
|
|
440
|
-
The `var` method is the most convenient way to show variables and their values
|
544
|
+
The `var` method is the most convenient way to show variables and their values.
|
545
|
+
Let's let `byebug` to help us with it.
|
441
546
|
|
442
547
|
```
|
443
|
-
var
|
444
|
-
|
445
|
-
|
446
|
-
|
447
|
-
|
548
|
+
(byebug) help var
|
549
|
+
v[ar] cl[ass] show class variables of self
|
550
|
+
v[ar] const <object> show constants of object
|
551
|
+
v[ar] g[lobal] show global variables
|
552
|
+
v[ar] i[nstance] <object> show instance variables of object
|
553
|
+
v[ar] l[ocal] show local variables
|
448
554
|
```
|
449
555
|
|
450
|
-
This is a great way to inspect the values of the current context variables. For
|
556
|
+
This is a great way to inspect the values of the current context variables. For
|
557
|
+
example, to check that we have no local variables currently defined.
|
451
558
|
|
452
559
|
```
|
453
|
-
(
|
454
|
-
|
560
|
+
(byebug) var local
|
561
|
+
(byebug)
|
455
562
|
```
|
456
563
|
|
457
564
|
You can also inspect for an object method this way:
|
458
565
|
|
459
566
|
```
|
460
|
-
(
|
461
|
-
@
|
567
|
+
(byebug) var instance Article.new
|
568
|
+
@_start_transaction_state = {}
|
569
|
+
@aggregation_cache = {}
|
570
|
+
@association_cache = {}
|
571
|
+
@attributes = {"id"=>nil, "created_at"=>nil, "updated_at"=>nil}
|
462
572
|
@attributes_cache = {}
|
463
|
-
@
|
573
|
+
@changed_attributes = nil
|
574
|
+
...
|
464
575
|
```
|
465
576
|
|
466
|
-
TIP: The commands `p` (print) and `pp` (pretty print) can be used to evaluate
|
577
|
+
TIP: The commands `p` (print) and `pp` (pretty print) can be used to evaluate
|
578
|
+
Ruby expressions and display the value of variables to the console.
|
467
579
|
|
468
|
-
You can use also `display` to start watching variables. This is a good way of
|
580
|
+
You can use also `display` to start watching variables. This is a good way of
|
581
|
+
tracking the values of a variable while the execution goes on.
|
469
582
|
|
470
583
|
```
|
471
|
-
(
|
472
|
-
1: @
|
584
|
+
(byebug) display @articles
|
585
|
+
1: @articles = nil
|
473
586
|
```
|
474
587
|
|
475
|
-
The variables inside the displaying list will be printed with their values after
|
588
|
+
The variables inside the displaying list will be printed with their values after
|
589
|
+
you move in the stack. To stop displaying a variable use `undisplay _n_` where
|
590
|
+
_n_ is the variable number (1 in the last example).
|
476
591
|
|
477
592
|
### Step by Step
|
478
593
|
|
479
|
-
Now you should know where you are in the running trace and be able to print the
|
594
|
+
Now you should know where you are in the running trace and be able to print the
|
595
|
+
available variables. But lets continue and move on with the application
|
596
|
+
execution.
|
480
597
|
|
481
|
-
Use `step` (abbreviated `s`) to continue running your program until the next
|
598
|
+
Use `step` (abbreviated `s`) to continue running your program until the next
|
599
|
+
logical stopping point and return control to the debugger.
|
482
600
|
|
483
|
-
|
601
|
+
You may also use `next` which is similar to step, but function or method calls
|
602
|
+
that appear within the line of code are executed without stopping.
|
484
603
|
|
485
|
-
You
|
604
|
+
TIP: You can also use `step n` or `next n` to move forwards `n` steps at once.
|
486
605
|
|
487
|
-
The difference between `next` and `step` is that `step` stops at the next line
|
606
|
+
The difference between `next` and `step` is that `step` stops at the next line
|
607
|
+
of code executed, doing just a single step, while `next` moves to the next line
|
608
|
+
without descending inside methods.
|
488
609
|
|
489
|
-
For example, consider
|
610
|
+
For example, consider the following situation:
|
490
611
|
|
491
612
|
```ruby
|
492
|
-
|
493
|
-
|
494
|
-
has_many :comments
|
613
|
+
Started GET "/" for 127.0.0.1 at 2014-04-11 13:39:23 +0200
|
614
|
+
Processing by ArticlesController#index as HTML
|
495
615
|
|
496
|
-
|
497
|
-
|
498
|
-
|
499
|
-
|
500
|
-
|
616
|
+
[1, 8] in /home/davidr/Proyectos/test_app/app/models/article.rb
|
617
|
+
1: class Article < ActiveRecord::Base
|
618
|
+
2:
|
619
|
+
3: def self.find_recent(limit = 10)
|
620
|
+
4: byebug
|
621
|
+
=> 5: where('created_at > ?', 1.week.ago).limit(limit)
|
622
|
+
6: end
|
623
|
+
7:
|
624
|
+
8: end
|
625
|
+
|
626
|
+
(byebug)
|
501
627
|
```
|
502
628
|
|
503
|
-
|
629
|
+
If we use `next`, we want go deep inside method calls. Instead, byebug will go
|
630
|
+
to the next line within the same context. In this case, this is the last line of
|
631
|
+
the method, so `byebug` will jump to next next line of the previous frame.
|
504
632
|
|
505
633
|
```
|
506
|
-
|
507
|
-
|
508
|
-
>> require "debugger"
|
509
|
-
=> []
|
510
|
-
>> author = Author.first
|
511
|
-
=> #<Author id: 1, first_name: "Bob", last_name: "Smith", created_at: "2008-07-31 12:46:10", updated_at: "2008-07-31 12:46:10">
|
512
|
-
>> author.find_recent_comments
|
513
|
-
/PathTo/project/app/models/author.rb:11
|
514
|
-
)
|
515
|
-
```
|
634
|
+
(byebug) next
|
635
|
+
Next went up a frame because previous frame finished
|
516
636
|
|
517
|
-
|
637
|
+
[4, 13] in /PathTo/project/test_app/app/controllers/articles_controller.rb
|
638
|
+
4: # GET /articles
|
639
|
+
5: # GET /articles.json
|
640
|
+
6: def index
|
641
|
+
7: @articles = Article.find_recent
|
642
|
+
8:
|
643
|
+
=> 9: respond_to do |format|
|
644
|
+
10: format.html # index.html.erb
|
645
|
+
11: format.json { render json: @articles }
|
646
|
+
12: end
|
647
|
+
13: end
|
518
648
|
|
519
|
-
|
520
|
-
(rdb:1) list
|
521
|
-
[2, 9] in /PathTo/project/app/models/author.rb
|
522
|
-
2 has_one :editorial
|
523
|
-
3 has_many :comments
|
524
|
-
4
|
525
|
-
5 def find_recent_comments(limit = 10)
|
526
|
-
6 debugger
|
527
|
-
=> 7 @recent_comments ||= comments.where("created_at > ?", 1.week.ago).limit(limit)
|
528
|
-
8 end
|
529
|
-
9 end
|
649
|
+
(byebug)
|
530
650
|
```
|
531
651
|
|
532
|
-
|
652
|
+
If we use `step` in the same situation, we will literally go the next ruby
|
653
|
+
instruction to be executed. In this case, the activesupport's `week` method.
|
533
654
|
|
534
655
|
```
|
535
|
-
(
|
536
|
-
@attributes = {"updated_at"=>"2008-07-31 12:46:10", "id"=>"1", "first_name"=>"Bob", "las...
|
537
|
-
@attributes_cache = {}
|
538
|
-
```
|
656
|
+
(byebug) step
|
539
657
|
|
540
|
-
|
658
|
+
[50, 59] in /PathToGems/activesupport-4.2.0/lib/active_support/core_ext/numeric/time.rb
|
659
|
+
50: ActiveSupport::Duration.new(self * 24.hours, [[:days, self]])
|
660
|
+
51: end
|
661
|
+
52: alias :day :days
|
662
|
+
53:
|
663
|
+
54: def weeks
|
664
|
+
=> 55: ActiveSupport::Duration.new(self * 7.days, [[:days, self * 7]])
|
665
|
+
56: end
|
666
|
+
57: alias :week :weeks
|
667
|
+
58:
|
668
|
+
59: def fortnights
|
541
669
|
|
670
|
+
(byebug)
|
542
671
|
```
|
543
|
-
(rdb:1) next
|
544
|
-
/PathTo/project/app/models/author.rb:12
|
545
|
-
@recent_comments
|
546
|
-
(rdb:1) var instance
|
547
|
-
@attributes = {"updated_at"=>"2008-07-31 12:46:10", "id"=>"1", "first_name"=>"Bob", "las...
|
548
|
-
@attributes_cache = {}
|
549
|
-
@comments = []
|
550
|
-
@recent_comments = []
|
551
|
-
```
|
552
|
-
|
553
|
-
Now you can see that the `@comments` relationship was loaded and @recent_comments defined because the line was executed.
|
554
672
|
|
555
|
-
|
673
|
+
This is one of the best ways to find bugs in your code, or perhaps in Ruby on
|
674
|
+
Rails.
|
556
675
|
|
557
676
|
### Breakpoints
|
558
677
|
|
559
|
-
A breakpoint makes your application stop whenever a certain point in the program
|
678
|
+
A breakpoint makes your application stop whenever a certain point in the program
|
679
|
+
is reached. The debugger shell is invoked in that line.
|
560
680
|
|
561
|
-
You can add breakpoints dynamically with the command `break` (or just `b`).
|
681
|
+
You can add breakpoints dynamically with the command `break` (or just `b`).
|
682
|
+
There are 3 possible ways of adding breakpoints manually:
|
562
683
|
|
563
684
|
* `break line`: set breakpoint in the _line_ in the current source file.
|
564
|
-
* `break file:line [if expression]`: set breakpoint in the _line_ number inside
|
565
|
-
|
685
|
+
* `break file:line [if expression]`: set breakpoint in the _line_ number inside
|
686
|
+
the _file_. If an _expression_ is given it must evaluated to _true_ to fire up
|
687
|
+
the debugger.
|
688
|
+
* `break class(.|\#)method [if expression]`: set breakpoint in _method_ (. and
|
689
|
+
\# for class and instance method respectively) defined in _class_. The
|
690
|
+
_expression_ works the same way as with file:line.
|
691
|
+
|
692
|
+
|
693
|
+
For example, in the previous situation
|
566
694
|
|
567
695
|
```
|
568
|
-
|
569
|
-
|
696
|
+
[4, 13] in /PathTo/project/app/controllers/articles_controller.rb
|
697
|
+
4: # GET /articles
|
698
|
+
5: # GET /articles.json
|
699
|
+
6: def index
|
700
|
+
7: @articles = Article.find_recent
|
701
|
+
8:
|
702
|
+
=> 9: respond_to do |format|
|
703
|
+
10: format.html # index.html.erb
|
704
|
+
11: format.json { render json: @articles }
|
705
|
+
12: end
|
706
|
+
13: end
|
707
|
+
|
708
|
+
(byebug) break 11
|
709
|
+
Created breakpoint 1 at /PathTo/project/app/controllers/articles_controller.rb:11
|
710
|
+
|
570
711
|
```
|
571
712
|
|
572
|
-
Use `info breakpoints _n_` or `info break _n_` to list breakpoints. If you
|
713
|
+
Use `info breakpoints _n_` or `info break _n_` to list breakpoints. If you
|
714
|
+
supply a number, it lists that breakpoint. Otherwise it lists all breakpoints.
|
573
715
|
|
574
716
|
```
|
575
|
-
(
|
717
|
+
(byebug) info breakpoints
|
576
718
|
Num Enb What
|
577
|
-
|
719
|
+
1 y at /PathTo/project/app/controllers/articles_controller.rb:11
|
578
720
|
```
|
579
721
|
|
580
|
-
To delete breakpoints: use the command `delete _n_` to remove the breakpoint
|
722
|
+
To delete breakpoints: use the command `delete _n_` to remove the breakpoint
|
723
|
+
number _n_. If no number is specified, it deletes all breakpoints that are
|
724
|
+
currently active.
|
581
725
|
|
582
726
|
```
|
583
|
-
(
|
584
|
-
(
|
727
|
+
(byebug) delete 1
|
728
|
+
(byebug) info breakpoints
|
585
729
|
No breakpoints.
|
586
730
|
```
|
587
731
|
|
588
732
|
You can also enable or disable breakpoints:
|
589
733
|
|
590
|
-
* `enable breakpoints`: allow a list
|
734
|
+
* `enable breakpoints`: allow a _breakpoints_ list or all of them if no list is
|
735
|
+
specified, to stop your program. This is the default state when you create a
|
736
|
+
breakpoint.
|
591
737
|
* `disable breakpoints`: the _breakpoints_ will have no effect on your program.
|
592
738
|
|
593
739
|
### Catching Exceptions
|
594
740
|
|
595
|
-
The command `catch exception-name` (or just `cat exception-name`) can be used to
|
741
|
+
The command `catch exception-name` (or just `cat exception-name`) can be used to
|
742
|
+
intercept an exception of type _exception-name_ when there would otherwise be no
|
743
|
+
handler for it.
|
596
744
|
|
597
745
|
To list all active catchpoints use `catch`.
|
598
746
|
|
599
747
|
### Resuming Execution
|
600
748
|
|
601
|
-
There are two ways to resume execution of an application that is stopped in the
|
602
|
-
|
603
|
-
|
604
|
-
* `
|
749
|
+
There are two ways to resume execution of an application that is stopped in the
|
750
|
+
debugger:
|
751
|
+
|
752
|
+
* `continue` [line-specification] \(or `c`): resume program execution, at the
|
753
|
+
address where your script last stopped; any breakpoints set at that address are
|
754
|
+
bypassed. The optional argument line-specification allows you to specify a line
|
755
|
+
number to set a one-time breakpoint which is deleted when that breakpoint is
|
756
|
+
reached.
|
757
|
+
* `finish` [frame-number] \(or `fin`): execute until the selected stack frame
|
758
|
+
returns. If no frame number is given, the application will run until the
|
759
|
+
currently selected frame returns. The currently selected frame starts out the
|
760
|
+
most-recent frame or 0 if no frame positioning (e.g up, down or frame) has been
|
761
|
+
performed. If a frame number is given it will run until the specified frame
|
762
|
+
returns.
|
605
763
|
|
606
764
|
### Editing
|
607
765
|
|
608
766
|
Two commands allow you to open code from the debugger into an editor:
|
609
767
|
|
610
|
-
* `edit [file:line]`: edit _file_ using the editor specified by the EDITOR
|
611
|
-
|
768
|
+
* `edit [file:line]`: edit _file_ using the editor specified by the EDITOR
|
769
|
+
environment variable. A specific _line_ can also be given.
|
612
770
|
|
613
771
|
### Quitting
|
614
772
|
|
615
|
-
To exit the debugger, use the `quit` command (abbreviated `q`), or its alias
|
773
|
+
To exit the debugger, use the `quit` command (abbreviated `q`), or its alias
|
774
|
+
`exit`.
|
616
775
|
|
617
|
-
A simple quit tries to terminate all threads in effect. Therefore your server
|
776
|
+
A simple quit tries to terminate all threads in effect. Therefore your server
|
777
|
+
will be stopped and you will have to start it again.
|
618
778
|
|
619
779
|
### Settings
|
620
780
|
|
621
|
-
|
622
|
-
|
623
|
-
* `set reload`: Reload source code when changed.
|
624
|
-
* `set autolist`: Execute `list` command on every breakpoint.
|
625
|
-
* `set listsize _n_`: Set number of source lines to list by default to _n_.
|
626
|
-
* `set forcestep`: Make sure the `next` and `step` commands always move to a new line
|
781
|
+
`byebug` has a few available options to tweak its behaviour:
|
627
782
|
|
628
|
-
|
783
|
+
* `set autoreload`: Reload source code when changed (default: true).
|
784
|
+
* `set autolist`: Execute `list` command on every breakpoint (default: true).
|
785
|
+
* `set listsize _n_`: Set number of source lines to list by default to _n_
|
786
|
+
(default: 10)
|
787
|
+
* `set forcestep`: Make sure the `next` and `step` commands always move to a new
|
788
|
+
line.
|
629
789
|
|
630
|
-
|
790
|
+
You can see the full list by using `help set`. Use `help set _subcommand_` to
|
791
|
+
learn about a particular `set` command.
|
631
792
|
|
632
|
-
|
793
|
+
TIP: You can save these settings in an `.byebugrc` file in your home directory.
|
794
|
+
The debugger reads these global settings when it starts. For example:
|
633
795
|
|
634
796
|
```bash
|
635
|
-
set autolist
|
636
797
|
set forcestep
|
637
798
|
set listsize 25
|
638
799
|
```
|
@@ -640,33 +801,59 @@ set listsize 25
|
|
640
801
|
Debugging Memory Leaks
|
641
802
|
----------------------
|
642
803
|
|
643
|
-
A Ruby application (on Rails or not), can leak memory - either in the Ruby code
|
804
|
+
A Ruby application (on Rails or not), can leak memory - either in the Ruby code
|
805
|
+
or at the C code level.
|
644
806
|
|
645
|
-
In this section, you will learn how to find and fix such leaks by using tool
|
807
|
+
In this section, you will learn how to find and fix such leaks by using tool
|
808
|
+
such as Valgrind.
|
646
809
|
|
647
810
|
### Valgrind
|
648
811
|
|
649
|
-
[Valgrind](http://valgrind.org/) is a Linux-only application for detecting
|
812
|
+
[Valgrind](http://valgrind.org/) is a Linux-only application for detecting
|
813
|
+
C-based memory leaks and race conditions.
|
650
814
|
|
651
|
-
There are Valgrind tools that can automatically detect many memory management
|
815
|
+
There are Valgrind tools that can automatically detect many memory management
|
816
|
+
and threading bugs, and profile your programs in detail. For example, if a C
|
817
|
+
extension in the interpreter calls `malloc()` but doesn't properly call
|
818
|
+
`free()`, this memory won't be available until the app terminates.
|
652
819
|
|
653
|
-
For further information on how to install Valgrind and use with Ruby, refer to
|
820
|
+
For further information on how to install Valgrind and use with Ruby, refer to
|
821
|
+
[Valgrind and Ruby](http://blog.evanweaver.com/articles/2008/02/05/valgrind-and-ruby/)
|
822
|
+
by Evan Weaver.
|
654
823
|
|
655
824
|
Plugins for Debugging
|
656
825
|
---------------------
|
657
826
|
|
658
|
-
There are some Rails plugins to help you to find errors and debug your
|
659
|
-
|
660
|
-
|
661
|
-
* [
|
662
|
-
|
663
|
-
|
827
|
+
There are some Rails plugins to help you to find errors and debug your
|
828
|
+
application. Here is a list of useful plugins for debugging:
|
829
|
+
|
830
|
+
* [Footnotes](https://github.com/josevalim/rails-footnotes) Every Rails page has
|
831
|
+
footnotes that give request information and link back to your source via
|
832
|
+
TextMate.
|
833
|
+
* [Query Trace](https://github.com/ntalbott/query_trace/tree/master) Adds query
|
834
|
+
origin tracing to your logs.
|
835
|
+
* [Query Reviewer](https://github.com/nesquena/query_reviewer) This rails plugin
|
836
|
+
not only runs "EXPLAIN" before each of your select queries in development, but
|
837
|
+
provides a small DIV in the rendered output of each page with the summary of
|
838
|
+
warnings for each query that it analyzed.
|
839
|
+
* [Exception Notifier](https://github.com/smartinez87/exception_notification/tree/master)
|
840
|
+
Provides a mailer object and a default set of templates for sending email
|
841
|
+
notifications when errors occur in a Rails application.
|
842
|
+
* [Better Errors](https://github.com/charliesome/better_errors) Replaces the
|
843
|
+
standard Rails error page with a new one containing more contextual information,
|
844
|
+
like source code and variable inspection.
|
845
|
+
* [RailsPanel](https://github.com/dejan/rails_panel) Chrome extension for Rails
|
846
|
+
development that will end your tailing of development.log. Have all information
|
847
|
+
about your Rails app requests in the browser - in the Developer Tools panel.
|
848
|
+
Provides insight to db/rendering/total times, parameter list, rendered views and
|
849
|
+
more.
|
664
850
|
|
665
851
|
References
|
666
852
|
----------
|
667
853
|
|
668
854
|
* [ruby-debug Homepage](http://bashdb.sourceforge.net/ruby-debug/home-page.html)
|
669
855
|
* [debugger Homepage](https://github.com/cldwalker/debugger)
|
856
|
+
* [byebug Homepage](https://github.com/deivid-rodriguez/byebug)
|
670
857
|
* [Article: Debugging a Rails application with ruby-debug](http://www.sitepoint.com/debug-rails-app-ruby-debug/)
|
671
858
|
* [Ryan Bates' debugging ruby (revised) screencast](http://railscasts.com/episodes/54-debugging-ruby-revised)
|
672
859
|
* [Ryan Bates' stack trace screencast](http://railscasts.com/episodes/24-the-stack-trace)
|