rails 2.2.3 → 2.3.2

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of rails might be problematic. Click here for more details.

Files changed (229) hide show
  1. data/CHANGELOG +272 -191
  2. data/MIT-LICENSE +1 -1
  3. data/README +6 -19
  4. data/Rakefile +24 -71
  5. data/bin/rails +1 -0
  6. data/builtin/rails_info/rails/info.rb +12 -8
  7. data/configs/databases/mysql.yml +3 -0
  8. data/configs/initializers/backtrace_silencers.rb +7 -0
  9. data/configs/initializers/new_rails_defaults.rb +2 -0
  10. data/configs/initializers/session_store.rb +15 -0
  11. data/dispatches/config.ru +7 -0
  12. data/doc/README_FOR_APP +2 -5
  13. data/environments/boot.rb +1 -0
  14. data/environments/environment.rb +14 -48
  15. data/environments/production.rb +11 -7
  16. data/environments/test.rb +6 -0
  17. data/helpers/application_controller.rb +10 -0
  18. data/helpers/test_helper.rb +1 -1
  19. data/html/500.html +0 -3
  20. data/html/index.html +1 -0
  21. data/lib/commands/about.rb +1 -1
  22. data/lib/commands/dbconsole.rb +18 -2
  23. data/lib/commands/plugin.rb +4 -3
  24. data/lib/commands/runner.rb +13 -7
  25. data/lib/commands/server.rb +98 -33
  26. data/lib/console_app.rb +4 -4
  27. data/lib/console_with_helpers.rb +2 -23
  28. data/lib/dispatcher.rb +1 -1
  29. data/lib/fcgi_handler.rb +12 -10
  30. data/lib/initializer.rb +108 -41
  31. data/lib/rails/backtrace_cleaner.rb +54 -0
  32. data/lib/rails/gem_dependency.rb +124 -94
  33. data/lib/rails/plugin.rb +49 -6
  34. data/lib/rails/plugin/loader.rb +66 -27
  35. data/lib/rails/plugin/locator.rb +1 -1
  36. data/lib/rails/rack.rb +3 -1
  37. data/lib/rails/rack/debugger.rb +21 -0
  38. data/lib/rails/rack/log_tailer.rb +35 -0
  39. data/lib/rails/rack/metal.rb +51 -0
  40. data/lib/rails/rack/static.rb +15 -4
  41. data/lib/rails/version.rb +2 -2
  42. data/lib/rails_generator/base.rb +3 -0
  43. data/lib/rails_generator/commands.rb +8 -3
  44. data/lib/rails_generator/generators/applications/app/app_generator.rb +210 -136
  45. data/lib/rails_generator/generators/applications/app/scm/git.rb +16 -0
  46. data/lib/rails_generator/generators/applications/app/scm/scm.rb +8 -0
  47. data/lib/rails_generator/generators/applications/app/scm/svn.rb +7 -0
  48. data/lib/rails_generator/generators/applications/app/template_runner.rb +401 -0
  49. data/lib/rails_generator/generators/components/controller/USAGE +12 -11
  50. data/lib/rails_generator/generators/components/controller/controller_generator.rb +7 -1
  51. data/lib/rails_generator/generators/components/controller/templates/helper_test.rb +4 -0
  52. data/lib/rails_generator/generators/components/helper/USAGE +24 -0
  53. data/lib/rails_generator/generators/components/helper/helper_generator.rb +25 -0
  54. data/lib/rails_generator/generators/components/helper/templates/helper.rb +2 -0
  55. data/lib/rails_generator/generators/components/helper/templates/helper_test.rb +4 -0
  56. data/lib/rails_generator/generators/components/metal/USAGE +8 -0
  57. data/lib/rails_generator/generators/components/metal/metal_generator.rb +8 -0
  58. data/lib/rails_generator/generators/components/metal/templates/metal.rb +12 -0
  59. data/lib/rails_generator/generators/components/resource/USAGE +2 -2
  60. data/lib/rails_generator/generators/components/resource/resource_generator.rb +2 -0
  61. data/lib/rails_generator/generators/components/resource/templates/helper_test.rb +4 -0
  62. data/lib/rails_generator/generators/components/scaffold/scaffold_generator.rb +2 -0
  63. data/lib/rails_generator/generators/components/scaffold/templates/controller.rb +1 -1
  64. data/lib/rails_generator/generators/components/scaffold/templates/functional_test.rb +4 -4
  65. data/lib/rails_generator/generators/components/scaffold/templates/helper_test.rb +4 -0
  66. data/lib/rails_generator/generators/components/scaffold/templates/layout.html.erb +1 -1
  67. data/lib/rails_generator/generators/components/scaffold/templates/view_edit.html.erb +2 -2
  68. data/lib/rails_generator/generators/components/scaffold/templates/view_index.html.erb +2 -2
  69. data/lib/rails_generator/generators/components/scaffold/templates/view_new.html.erb +2 -2
  70. data/lib/rails_generator/generators/components/scaffold/templates/view_show.html.erb +1 -1
  71. data/lib/rails_generator/secret_key_generator.rb +2 -0
  72. data/lib/tasks/databases.rake +13 -6
  73. data/lib/tasks/documentation.rake +2 -9
  74. data/lib/tasks/framework.rake +26 -1
  75. data/lib/tasks/gems.rake +33 -48
  76. data/lib/tasks/middleware.rake +7 -0
  77. data/lib/tasks/misc.rake +7 -0
  78. data/lib/tasks/statistics.rake +0 -1
  79. data/lib/tasks/testing.rake +5 -5
  80. data/lib/tasks/tmp.rake +2 -2
  81. data/lib/test_help.rb +16 -8
  82. metadata +92 -159
  83. data/bin/performance/request +0 -3
  84. data/bin/process/inspector +0 -3
  85. data/bin/process/reaper +0 -3
  86. data/bin/process/spawner +0 -3
  87. data/config.ru +0 -17
  88. data/configs/apache.conf +0 -40
  89. data/configs/lighttpd.conf +0 -54
  90. data/doc/guides/html/2_2_release_notes.html +0 -1185
  91. data/doc/guides/html/actioncontroller_basics.html +0 -1270
  92. data/doc/guides/html/activerecord_validations_callbacks.html +0 -749
  93. data/doc/guides/html/association_basics.html +0 -2585
  94. data/doc/guides/html/authors.html +0 -240
  95. data/doc/guides/html/benchmarking_and_profiling.html +0 -1018
  96. data/doc/guides/html/caching_with_rails.html +0 -583
  97. data/doc/guides/html/command_line.html +0 -434
  98. data/doc/guides/html/configuring.html +0 -438
  99. data/doc/guides/html/creating_plugins.html +0 -1594
  100. data/doc/guides/html/debugging_rails_applications.html +0 -1175
  101. data/doc/guides/html/finders.html +0 -1090
  102. data/doc/guides/html/form_helpers.html +0 -638
  103. data/doc/guides/html/getting_started_with_rails.html +0 -2066
  104. data/doc/guides/html/index.html +0 -349
  105. data/doc/guides/html/layouts_and_rendering.html +0 -1406
  106. data/doc/guides/html/migrations.html +0 -921
  107. data/doc/guides/html/routing_outside_in.html +0 -2213
  108. data/doc/guides/html/security.html +0 -1346
  109. data/doc/guides/html/testing_rails_applications.html +0 -1859
  110. data/doc/guides/source/2_2_release_notes.txt +0 -435
  111. data/doc/guides/source/actioncontroller_basics/changelog.txt +0 -5
  112. data/doc/guides/source/actioncontroller_basics/cookies.txt +0 -34
  113. data/doc/guides/source/actioncontroller_basics/csrf.txt +0 -32
  114. data/doc/guides/source/actioncontroller_basics/filters.txt +0 -119
  115. data/doc/guides/source/actioncontroller_basics/http_auth.txt +0 -24
  116. data/doc/guides/source/actioncontroller_basics/index.txt +0 -40
  117. data/doc/guides/source/actioncontroller_basics/introduction.txt +0 -9
  118. data/doc/guides/source/actioncontroller_basics/methods.txt +0 -39
  119. data/doc/guides/source/actioncontroller_basics/parameter_filtering.txt +0 -14
  120. data/doc/guides/source/actioncontroller_basics/params.txt +0 -93
  121. data/doc/guides/source/actioncontroller_basics/request_response_objects.txt +0 -43
  122. data/doc/guides/source/actioncontroller_basics/rescue.txt +0 -67
  123. data/doc/guides/source/actioncontroller_basics/session.txt +0 -187
  124. data/doc/guides/source/actioncontroller_basics/streaming.txt +0 -91
  125. data/doc/guides/source/actioncontroller_basics/verification.txt +0 -40
  126. data/doc/guides/source/active_record_basics.txt +0 -181
  127. data/doc/guides/source/activerecord_validations_callbacks.txt +0 -404
  128. data/doc/guides/source/association_basics.txt +0 -1840
  129. data/doc/guides/source/authors.txt +0 -39
  130. data/doc/guides/source/benchmarking_and_profiling/appendix.txt +0 -95
  131. data/doc/guides/source/benchmarking_and_profiling/digging_deeper.txt +0 -105
  132. data/doc/guides/source/benchmarking_and_profiling/edge_rails_features.txt +0 -185
  133. data/doc/guides/source/benchmarking_and_profiling/gameplan.txt +0 -27
  134. data/doc/guides/source/benchmarking_and_profiling/index.txt +0 -242
  135. data/doc/guides/source/benchmarking_and_profiling/rubyprof.txt +0 -179
  136. data/doc/guides/source/benchmarking_and_profiling/statistics.txt +0 -57
  137. data/doc/guides/source/caching_with_rails.txt +0 -367
  138. data/doc/guides/source/command_line.txt +0 -147
  139. data/doc/guides/source/configuring.txt +0 -225
  140. data/doc/guides/source/creating_plugins/acts_as_yaffle.txt +0 -191
  141. data/doc/guides/source/creating_plugins/appendix.txt +0 -46
  142. data/doc/guides/source/creating_plugins/controllers.txt +0 -59
  143. data/doc/guides/source/creating_plugins/core_ext.txt +0 -123
  144. data/doc/guides/source/creating_plugins/custom_route.txt +0 -69
  145. data/doc/guides/source/creating_plugins/gem.txt +0 -1
  146. data/doc/guides/source/creating_plugins/generator_method.txt +0 -89
  147. data/doc/guides/source/creating_plugins/helpers.txt +0 -51
  148. data/doc/guides/source/creating_plugins/index.txt +0 -52
  149. data/doc/guides/source/creating_plugins/migration_generator.txt +0 -156
  150. data/doc/guides/source/creating_plugins/models.txt +0 -76
  151. data/doc/guides/source/creating_plugins/odds_and_ends.txt +0 -69
  152. data/doc/guides/source/creating_plugins/test_setup.txt +0 -230
  153. data/doc/guides/source/debugging_rails_applications.txt +0 -733
  154. data/doc/guides/source/finders.txt +0 -668
  155. data/doc/guides/source/form_helpers.txt +0 -345
  156. data/doc/guides/source/getting_started_with_rails.txt +0 -1256
  157. data/doc/guides/source/images/belongs_to.png +0 -0
  158. data/doc/guides/source/images/bullet.gif +0 -0
  159. data/doc/guides/source/images/csrf.png +0 -0
  160. data/doc/guides/source/images/habtm.png +0 -0
  161. data/doc/guides/source/images/has_many.png +0 -0
  162. data/doc/guides/source/images/has_many_through.png +0 -0
  163. data/doc/guides/source/images/has_one.png +0 -0
  164. data/doc/guides/source/images/has_one_through.png +0 -0
  165. data/doc/guides/source/images/header_backdrop.png +0 -0
  166. data/doc/guides/source/images/icons/README +0 -5
  167. data/doc/guides/source/images/icons/callouts/1.png +0 -0
  168. data/doc/guides/source/images/icons/callouts/10.png +0 -0
  169. data/doc/guides/source/images/icons/callouts/11.png +0 -0
  170. data/doc/guides/source/images/icons/callouts/12.png +0 -0
  171. data/doc/guides/source/images/icons/callouts/13.png +0 -0
  172. data/doc/guides/source/images/icons/callouts/14.png +0 -0
  173. data/doc/guides/source/images/icons/callouts/15.png +0 -0
  174. data/doc/guides/source/images/icons/callouts/2.png +0 -0
  175. data/doc/guides/source/images/icons/callouts/3.png +0 -0
  176. data/doc/guides/source/images/icons/callouts/4.png +0 -0
  177. data/doc/guides/source/images/icons/callouts/5.png +0 -0
  178. data/doc/guides/source/images/icons/callouts/6.png +0 -0
  179. data/doc/guides/source/images/icons/callouts/7.png +0 -0
  180. data/doc/guides/source/images/icons/callouts/8.png +0 -0
  181. data/doc/guides/source/images/icons/callouts/9.png +0 -0
  182. data/doc/guides/source/images/icons/caution.png +0 -0
  183. data/doc/guides/source/images/icons/example.png +0 -0
  184. data/doc/guides/source/images/icons/home.png +0 -0
  185. data/doc/guides/source/images/icons/important.png +0 -0
  186. data/doc/guides/source/images/icons/next.png +0 -0
  187. data/doc/guides/source/images/icons/note.png +0 -0
  188. data/doc/guides/source/images/icons/prev.png +0 -0
  189. data/doc/guides/source/images/icons/tip.png +0 -0
  190. data/doc/guides/source/images/icons/up.png +0 -0
  191. data/doc/guides/source/images/icons/warning.png +0 -0
  192. data/doc/guides/source/images/polymorphic.png +0 -0
  193. data/doc/guides/source/images/rails_logo_remix.gif +0 -0
  194. data/doc/guides/source/images/ruby_on_rails_by_mike_rundle2.gif +0 -0
  195. data/doc/guides/source/images/session_fixation.png +0 -0
  196. data/doc/guides/source/index.txt +0 -118
  197. data/doc/guides/source/layouts_and_rendering.txt +0 -982
  198. data/doc/guides/source/migrations/anatomy_of_a_migration.txt +0 -85
  199. data/doc/guides/source/migrations/changelog.txt +0 -5
  200. data/doc/guides/source/migrations/creating_a_migration.txt +0 -109
  201. data/doc/guides/source/migrations/foreign_keys.txt +0 -8
  202. data/doc/guides/source/migrations/index.txt +0 -22
  203. data/doc/guides/source/migrations/rakeing_around.txt +0 -111
  204. data/doc/guides/source/migrations/scheming.txt +0 -47
  205. data/doc/guides/source/migrations/using_models_in_migrations.txt +0 -46
  206. data/doc/guides/source/migrations/writing_a_migration.txt +0 -159
  207. data/doc/guides/source/routing_outside_in.txt +0 -986
  208. data/doc/guides/source/security.txt +0 -984
  209. data/doc/guides/source/stylesheets/base.css +0 -358
  210. data/doc/guides/source/stylesheets/forms.css +0 -35
  211. data/doc/guides/source/stylesheets/more.css +0 -82
  212. data/doc/guides/source/templates/guides.html.erb +0 -97
  213. data/doc/guides/source/templates/inline.css +0 -165
  214. data/doc/guides/source/testing_rails_applications.txt +0 -995
  215. data/helpers/application.rb +0 -15
  216. data/lib/commands/performance/request.rb +0 -6
  217. data/lib/commands/process/inspector.rb +0 -68
  218. data/lib/commands/process/reaper.rb +0 -149
  219. data/lib/commands/process/spawner.rb +0 -219
  220. data/lib/commands/process/spinner.rb +0 -57
  221. data/lib/commands/servers/base.rb +0 -31
  222. data/lib/commands/servers/lighttpd.rb +0 -94
  223. data/lib/commands/servers/mongrel.rb +0 -69
  224. data/lib/commands/servers/new_mongrel.rb +0 -16
  225. data/lib/commands/servers/thin.rb +0 -25
  226. data/lib/commands/servers/webrick.rb +0 -66
  227. data/lib/rails/mongrel_server/commands.rb +0 -342
  228. data/lib/rails/mongrel_server/handler.rb +0 -55
  229. data/lib/rails/rack/logger.rb +0 -28
@@ -1,32 +0,0 @@
1
- == Request Forgery Protection ==
2
-
3
- Cross-site request forgery is a type of attack in which a site tricks a user into making requests on another site, possibly adding, modifying or deleting data on that site without the user's knowledge or permission. The first step to avoid this is to make sure all "destructive" actions (create, update and destroy) can only be accessed with non-GET requests. If you're following RESTful conventions you're already doing this. However, a malicious site can still send a non-GET request to your site quite easily, and that's where the request forgery protection comes in. As the name says, it protects from forged requests. The way this is done is to add a non-guessable token which is only known to your server to each request. This way, if a request comes in without the proper token, it will be denied access.
4
-
5
- If you generate a form like this:
6
-
7
- [source, ruby]
8
- -----------------------------------------
9
- <% form_for @user do |f| -%>
10
- <%= f.text_field :username %>
11
- <%= f.text_field :password -%>
12
- <% end -%>
13
- -----------------------------------------
14
-
15
- You will see how the token gets added as a hidden field:
16
-
17
- [source, html]
18
- -----------------------------------------
19
- <form action="/users/1" method="post">
20
- <div><!-- ... --><input type="hidden" value="67250ab105eb5ad10851c00a5621854a23af5489" name="authenticity_token"/></div>
21
- <!-- Fields -->
22
- </form>
23
- -----------------------------------------
24
-
25
- Rails adds this token to every form that's generated using the link:../form_helpers.html[form helpers], so most of the time you don't have to worry about it. If you're writing a form manually or need to add the token for another reason, it's available through the method `form_authenticity_token`:
26
-
27
- .Add a JavaScript variable containing the token for use with Ajax
28
- -----------------------------------------
29
- <%= javascript_tag "MyApp.authenticity_token = '#{form_authenticity_token}'" %>
30
- -----------------------------------------
31
-
32
- The link:../security.html[Security Guide] has more about this and a lot of other security-related issues that you should be aware of when developing a web application.
@@ -1,119 +0,0 @@
1
- == Filters ==
2
-
3
- Filters are methods that are run before, after or "around" a controller action. For example, one filter might check to see if the logged in user has the right credentials to access that particular controller or action. Filters are inherited, so if you set a filter on ApplicationController, it will be run on every controller in your application. A common, simple filter is one which requires that a user is logged in for an action to be run. You can define the filter method this way:
4
-
5
- [source, ruby]
6
- ---------------------------------
7
- class ApplicationController < ActionController::Base
8
-
9
- private
10
-
11
- def require_login
12
- unless logged_in?
13
- flash[:error] = "You must be logged in to access this section"
14
- redirect_to new_login_url # Prevents the current action from running
15
- end
16
- end
17
-
18
- # The logged_in? method simply returns true if the user is logged in and
19
- # false otherwise. It does this by "booleanizing" the current_user method
20
- # we created previously using a double ! operator. Note that this is not
21
- # common in Ruby and is discouraged unless you really mean to convert something
22
- # into true or false.
23
- def logged_in?
24
- !!current_user
25
- end
26
-
27
- end
28
- ---------------------------------
29
-
30
- The method simply stores an error message in the flash and redirects to the login form if the user is not logged in. If a before filter (a filter which is run before the action) renders or redirects, the action will not run. If there are additional filters scheduled to run after the rendering or redirecting filter, they are also cancelled. To use this filter in a controller, use the `before_filter` method:
31
-
32
- [source, ruby]
33
- ---------------------------------
34
- class ApplicationController < ActionController::Base
35
-
36
- before_filter :require_login
37
-
38
- end
39
- ---------------------------------
40
-
41
- In this example, the filter is added to ApplicationController and thus all controllers in the application. This will make everything in the application require the user to be logged in in order to use it. For obvious reasons (the user wouldn't be able to log in in the first place!), not all controllers or actions should require this. You can prevent this filter from running before particular actions with `skip_before_filter` :
42
-
43
- [source, ruby]
44
- ---------------------------------
45
- class LoginsController < Application
46
-
47
- skip_before_filter :require_login, :only => [:new, :create]
48
-
49
- end
50
- ---------------------------------
51
-
52
- Now, the +LoginsController+'s "new" and "create" actions will work as before without requiring the user to be logged in. The `:only` option is used to only skip this filter for these actions, and there is also an `:except` option which works the other way. These options can be used when adding filters too, so you can add a filter which only runs for selected actions in the first place.
53
-
54
- === After Filters and Around Filters ===
55
-
56
- In addition to the before filters, you can run filters after an action has run or both before and after. The after filter is similar to the before filter, but because the action has already been run it has access to the response data that's about to be sent to the client. Obviously, after filters can not stop the action from running. Around filters are responsible for running the action, but they can choose not to, which is the around filter's way of stopping it.
57
-
58
- [source, ruby]
59
- ---------------------------------
60
- # Example taken from the Rails API filter documentation:
61
- # http://api.rubyonrails.org/classes/ActionController/Filters/ClassMethods.html
62
- class ApplicationController < Application
63
-
64
- around_filter :catch_exceptions
65
-
66
- private
67
-
68
- def catch_exceptions
69
- yield
70
- rescue => exception
71
- logger.debug "Caught exception! #{exception}"
72
- raise
73
- end
74
-
75
- end
76
- ---------------------------------
77
-
78
- === Other Ways to Use Filters ===
79
-
80
- While the most common way to use filters is by creating private methods and using *_filter to add them, there are two other ways to do the same thing.
81
-
82
- The first is to use a block directly with the *_filter methods. The block receives the controller as an argument, and the `require_login` filter from above could be rewritten to use a block:
83
-
84
- [source, ruby]
85
- ---------------------------------
86
- class ApplicationController < ActionController::Base
87
-
88
- before_filter { |controller| redirect_to new_login_url unless controller.send(:logged_in?) }
89
-
90
- end
91
- ---------------------------------
92
-
93
- Note that the filter in this case uses `send` because the `logged_in?` method is private and the filter is not run in the scope of the controller. This is not the recommended way to implement this particular filter, but in more simple cases it might be useful.
94
-
95
- The second way is to use a class (actually, any object that responds to the right methods will do) to handle the filtering. This is useful in cases that are more complex than can not be implemented in a readable and reusable way using the two other methods. As an example, you could rewrite the login filter again to use a class:
96
-
97
- [source, ruby]
98
- ---------------------------------
99
- class ApplicationController < ActionController::Base
100
-
101
- before_filter LoginFilter
102
-
103
- end
104
-
105
- class LoginFilter
106
-
107
- def self.filter(controller)
108
- unless logged_in?
109
- controller.flash[:error] = "You must be logged in to access this section"
110
- controller.redirect_to controller.new_login_url
111
- end
112
- end
113
-
114
- end
115
- ---------------------------------
116
-
117
- Again, this is not an ideal example for this filter, because it's not run in the scope of the controller but gets the controller passed as an argument. The filter class has a class method `filter` which gets run before or after the action, depending on if it's a before or after filter. Classes used as around filters can also use the same `filter` method, which will get run in the same way. The method must `yield` to execute the action. Alternatively, it can have both a `before` and an `after` method that are run before and after the action.
118
-
119
- The Rails API documentation has link:http://api.rubyonrails.org/classes/ActionController/Filters/ClassMethods.html[more information on using filters].
@@ -1,24 +0,0 @@
1
- == HTTP Basic Authentication ==
2
-
3
- Rails comes with built-in HTTP Basic authentication. This is an authentication scheme that is supported by the majority of browsers and other HTTP clients. As an example, consider an administration section which will only be available by entering a username and a password into the browser's HTTP Basic dialog window. Using the built-in authentication is quite easy and only requires you to use one method, `authenticate_or_request_with_http_basic`.
4
-
5
- [source, ruby]
6
- -------------------------------------
7
- class AdminController < ApplicationController
8
-
9
- USERNAME, PASSWORD = "humbaba", "5baa61e4c9b93f3f0682250b6cf8331b7ee68fd8"
10
-
11
- before_filter :authenticate
12
-
13
- private
14
-
15
- def authenticate
16
- authenticate_or_request_with_http_basic do |username, password|
17
- username == USERNAME && Digest::SHA1.hexdigest(password) == PASSWORD
18
- end
19
- end
20
-
21
- end
22
- -------------------------------------
23
-
24
- With this in place, you can create namespaced controllers that inherit from AdminController. The before filter will thus be run for all actions in those controllers, protecting them with HTTP Basic authentication.
@@ -1,40 +0,0 @@
1
- Action Controller basics
2
- =======================
3
-
4
- In this guide you will learn how controllers work and how they fit into the request cycle in your application. After reading this guide, you will be able to:
5
-
6
- * Follow the flow of a request through a controller
7
- * Understand why and how to store data in the session or cookies
8
- * Work with filters to execute code during request processing
9
- * Use Action Controller's built-in HTTP authentication
10
- * Stream data directly to the user's browser
11
- * Filter sensitive parameters so they do not appear in the application's log
12
- * Deal with exceptions that may be raised during request processing
13
-
14
- include::introduction.txt[]
15
-
16
- include::methods.txt[]
17
-
18
- include::params.txt[]
19
-
20
- include::session.txt[]
21
-
22
- include::cookies.txt[]
23
-
24
- include::filters.txt[]
25
-
26
- include::verification.txt[]
27
-
28
- include::csrf.txt[]
29
-
30
- include::request_response_objects.txt[]
31
-
32
- include::http_auth.txt[]
33
-
34
- include::streaming.txt[]
35
-
36
- include::parameter_filtering.txt[]
37
-
38
- include::rescue.txt[]
39
-
40
- include::changelog.txt[]
@@ -1,9 +0,0 @@
1
- == What Does a Controller do? ==
2
-
3
- Action Controller is the C in MVC. After routing has determined which controller to use for a request, your controller is responsible for making sense of the request and producing the appropriate output. Luckily, Action Controller does most of the groundwork for you and uses smart conventions to make this as straight-forward as possible.
4
-
5
- For most conventional RESTful applications, the controller will receive the request (this is invisible to you as the developer), fetch or save data from a model and use a view to create HTML output. If your controller needs to do things a little differently, that's not a problem, this is just the most common way for a controller to work.
6
-
7
- A controller can thus be thought of as a middle man between models and views. It makes the model data available to the view so it can display that data to the user, and it saves or updates data from the user to the model.
8
-
9
- NOTE: For more details on the routing process, see link:../routing_outside_in.html[Rails Routing from the Outside In].
@@ -1,39 +0,0 @@
1
- == Methods and Actions ==
2
-
3
- A controller is a Ruby class which inherits from ApplicationController and has methods just like any other class. When your application receives a request, the routing will determine which controller and action to run, then Rails creates an instance of that controller and runs the public method with the same name as the action.
4
-
5
- [source, ruby]
6
- ----------------------------------------------
7
- class ClientsController < ApplicationController
8
-
9
- # Actions are public methods
10
- def new
11
- end
12
-
13
- # Action methods are responsible for producing output
14
- def edit
15
- end
16
-
17
- # Helper methods are private and can not be used as actions
18
- private
19
-
20
- def foo
21
- end
22
-
23
- end
24
- ----------------------------------------------
25
-
26
- There's no rule saying a method on a controller has to be an action; they may well be used for other purposes such as filters, which will be covered later in this guide.
27
-
28
- As an example, if a user goes to `/clients/new` in your application to add a new client, Rails will create an instance of ClientsController and run the `new` method. Note that the empty method from the example above could work just fine because Rails will by default render the `new.html.erb` view unless the action says otherwise. The `new` method could make available to the view a `@client` instance variable by creating a new Client:
29
-
30
- [source, ruby]
31
- ----------------------------------------------
32
- def new
33
- @client = Client.new
34
- end
35
- ----------------------------------------------
36
-
37
- The link:../layouts_and_rendering.html[Layouts & rendering guide] explains this in more detail.
38
-
39
- ApplicationController inherits from ActionController::Base, which defines a number of helpful methods. This guide will cover some of these, but if you're curious to see what's in there, you can see all of them in the API documentation or in the source itself.
@@ -1,14 +0,0 @@
1
- == Parameter Filtering ==
2
-
3
- Rails keeps a log file for each environment (development, test and production) in the "log" folder. These are extremely useful when debugging what's actually going on in your application, but in a live application you may not want every bit of information to be stored in the log file. The `filter_parameter_logging` method can be used to filter out sensitive information from the log. It works by replacing certain values in the `params` hash with "[FILTERED]" as they are written to the log. As an example, let's see how to filter all parameters with keys that include "password":
4
-
5
- [source, ruby]
6
- -------------------------
7
- class ApplicationController < ActionController::Base
8
-
9
- filter_parameter_logging :password
10
-
11
- end
12
- -------------------------
13
-
14
- The method works recursively through all levels of the params hash and takes an optional second parameter which is used as the replacement string if present. It can also take a block which receives each key in return and replaces those for which the block returns true.
@@ -1,93 +0,0 @@
1
- == Parameters ==
2
-
3
- You will probably want to access data sent in by the user or other parameters in your controller actions. There are two kinds of parameters possible in a web application. The first are parameters that are sent as part of the URL, called query string parameters. The query string is everything after "?" in the URL. The second type of parameter is usually referred to as POST data. This information usually comes from a HTML form which has been filled in by the user. It's called POST data because it can only be sent as part of an HTTP POST request. Rails does not make any distinction between query string parameters and POST parameters, and both are available in the `params` hash in your controller:
4
-
5
- [source, ruby]
6
- -------------------------------------
7
- class ClientsController < ActionController::Base
8
-
9
- # This action uses query string parameters because it gets run by a HTTP
10
- # GET request, but this does not make any difference to the way in which
11
- # the parameters are accessed. The URL for this action would look like this
12
- # in order to list activated clients: /clients?status=activated
13
- def index
14
- if params[:status] = "activated"
15
- @clients = Client.activated
16
- else
17
- @clients = Client.unativated
18
- end
19
- end
20
-
21
- # This action uses POST parameters. They are most likely coming from an HTML
22
- # form which the user has submitted. The URL for this RESTful request will
23
- # be "/clients", and the data will be sent as part of the request body.
24
- def create
25
- @client = Client.new(params[:client])
26
- if @client.save
27
- redirect_to @client
28
- else
29
- # This line overrides the default rendering behavior, which would have been
30
- # to render the "create" view.
31
- render :action => "new"
32
- end
33
- end
34
-
35
- end
36
- -------------------------------------
37
-
38
- === Hash and Array Parameters ===
39
-
40
- The params hash is not limited to one-dimensional keys and values. It can contain arrays and (nested) hashes. To send an array of values, append "[]" to the key name:
41
-
42
- -------------------------------------
43
- GET /clients?ids[]=1&ids[]=2&ids[]=3
44
- -------------------------------------
45
-
46
- NOTE: The actual URL in this example will be encoded as "/clients?ids%5b%5d=1&ids%5b%5d=2&ids%5b%5b=3" as [ and ] are not allowed in URLs. Most of the time you don't have to worry about this because the browser will take care of it for you, and Rails will decode it back when it receives it, but if you ever find yourself having to send those requests to the server manually you have to keep this in mind.
47
-
48
- The value of `params[:ids]` will now be `["1", "2", "3"]`. Note that parameter values are always strings; Rails makes no attempt to guess or cast the type.
49
-
50
- To send a hash you include the key name inside the brackets:
51
-
52
- -------------------------------------
53
- <form action="/clients" method="post">
54
- <input type="text" name="client[name]" value="Acme" />
55
- <input type="text" name="client[phone]" value="12345" />
56
- <input type="text" name="client[address][postcode]" value="12345" />
57
- <input type="text" name="client[address][city]" value="Carrot City" />
58
- </form>
59
- -------------------------------------
60
-
61
- The value of `params[:client]` when this form is submitted will be `{"name" => "Acme", "phone" => "12345", "address" => {"postcode" => "12345", "city" => "Carrot City"}}`. Note the nested hash in `params[:client][:address]`.
62
-
63
- Note that the params hash is actually an instance of HashWithIndifferentAccess from Active Support which is a subclass of Hash which lets you use symbols and strings interchangeably as keys.
64
-
65
- === Routing Parameters ===
66
-
67
- The `params` hash will always contain the `:controller` and `:action` keys, but you should use the methods `controller_name` and `action_name` instead to access these values. Any other parameters defined by the routing, such as `:id` will also be available. As an example, consider a listing of clients where the list can show either active or inactive clients. We can add a route which captures the `:status` parameter in a "pretty" URL:
68
-
69
- [source, ruby]
70
- ------------------------------------
71
- # ...
72
- map.connect "/clients/:status", :controller => "clients", :action => "index", :foo => "bar"
73
- # ...
74
- ------------------------------------
75
-
76
- In this case, when a user opens the URL `/clients/active`, `params[:status]` will be set to "active". When this route is used, `params[:foo]` will also be set to "bar" just like it was passed in the query string in the same way `params[:action]` will contain "index".
77
-
78
- === `default_url_options` ===
79
-
80
- You can set global default parameters that will be used when generating URLs with `default_url_options`. To do this, define a method with that name in your controller:
81
-
82
- ------------------------------------
83
- class ApplicationController < ActionController::Base
84
-
85
- #The options parameter is the hash passed in to +url_for+
86
- def default_url_options(options)
87
- {:locale => I18n.locale}
88
- end
89
-
90
- end
91
- ------------------------------------
92
-
93
- These options will be used as a starting-point when generating, so it's possible they'll be overridden by +url_for+. Because this method is defined in the controller, you can define it on ApplicationController so it would be used for all URL generation, or you could define it on only one controller for all URLs generated there.
@@ -1,43 +0,0 @@
1
- == The +request+ and +response+ Objects ==
2
-
3
- In every controller there are two accessor methods pointing to the request and the response objects associated with the request cycle that is currently in execution. The `request` method contains an instance of AbstractRequest and the `response` method returns a +response+ object representing what is going to be sent back to the client.
4
-
5
- === The +request+ Object ===
6
-
7
- The request object contains a lot of useful information about the request coming in from the client. To get a full list of the available methods, refer to the link:http://api.rubyonrails.org/classes/ActionController/AbstractRequest.html[API documentation]. Among the properties that you can access on this object are:
8
-
9
- * host - The hostname used for this request.
10
- * domain - The hostname without the first segment (usually "www").
11
- * format - The content type requested by the client.
12
- * method - The HTTP method used for the request.
13
- * get?, post?, put?, delete?, head? - Returns true if the HTTP method is get/post/put/delete/head.
14
- * headers - Returns a hash containing the headers associated with the request.
15
- * port - The port number (integer) used for the request.
16
- * protocol - The protocol used for the request.
17
- * query_string - The query string part of the URL - everything after "?".
18
- * remote_ip - The IP address of the client.
19
- * url - The entire URL used for the request.
20
-
21
- ==== +path_parameters+, +query_parameters+ and +request_parameters+ ====
22
-
23
- Rails collects all of the parameters sent along with the request in the `params` hash, whether they are sent as part of the query string or the post body. The request object has three accessors that give you access to these parameters depending on where they came from. The `query_parameters` hash contains parameters that were sent as part of the query string while the `request_parameters` hash contains parameters sent as part of the post body. The `path_parameters` hash contains parameters that were recognized by the routing as being part of the path leading to this particular controller and action.
24
-
25
- === The +response+ Object ===
26
-
27
- The response object is not usually used directly, but is built up during the execution of the action and rendering of the data that is being sent back to the user, but sometimes - like in an after filter - it can be useful to access the response directly. Some of these accessor methods also have setters, allowing you to change their values.
28
-
29
- * body - This is the string of data being sent back to the client. This is most often HTML.
30
- * status - The HTTP status code for the response, like 200 for a successful request or 404 for file not found.
31
- * location - The URL the client is being redirected to, if any.
32
- * content_type - The content type of the response.
33
- * charset - The character set being used for the response. Default is "utf8".
34
- * headers - Headers used for the response.
35
-
36
- ==== Setting Custom Headers ====
37
-
38
- If you want to set custom headers for a response then `response.headers` is the place to do it. The headers attribute is a hash which maps header names to their values, and Rails will set some of them - like "Content-Type" - automatically. If you want to add or change a header, just assign it to `headers` with the name and value:
39
-
40
- [source, ruby]
41
- -------------------------------------
42
- response.headers["Content-Type"] = "application/pdf"
43
- -------------------------------------
@@ -1,67 +0,0 @@
1
- == Rescue ==
2
-
3
- Most likely your application is going to contain bugs or otherwise throw an exception that needs to be handled. For example, if the user follows a link to a resource that no longer exists in the database, Active Record will throw the ActiveRecord::RecordNotFound exception. Rails' default exception handling displays a 500 Server Error message for all exceptions. If the request was made locally, a nice traceback and some added information gets displayed so you can figure out what went wrong and deal with it. If the request was remote Rails will just display a simple "500 Server Error" message to the user, or a "404 Not Found" if there was a routing error or a record could not be found. Sometimes you might want to customize how these errors are caught and how they're displayed to the user. There are several levels of exception handling available in a Rails application:
4
-
5
- === The Default 500 and 404 Templates ===
6
-
7
- By default a production application will render either a 404 or a 500 error message. These messages are contained in static HTML files in the `public` folder, in `404.html` and `500.html` respectively. You can customize these files to add some extra information and layout, but remember that they are static; i.e. you can't use RHTML or layouts in them, just plain HTML.
8
-
9
- === `rescue_from` ===
10
-
11
- If you want to do something a bit more elaborate when catching errors, you can use `rescue_from`, which handles exceptions of a certain type (or multiple types) in an entire controller and its subclasses. When an exception occurs which is caught by a +rescue_from+ directive, the exception object is passed to the handler. The handler can be a method or a Proc object passed to the `:with` option. You can also use a block directly instead of an explicit Proc object.
12
-
13
- Here's how you can use +rescue_from+ to intercept all ActiveRecord::RecordNotFound errors and do something with them.
14
-
15
- [source, ruby]
16
- -----------------------------------
17
- class ApplicationController < ActionController::Base
18
-
19
- rescue_from ActiveRecord::RecordNotFound, :with => :record_not_found
20
-
21
- private
22
-
23
- def record_not_found
24
- render :text => "404 Not Found", :status => 404
25
- end
26
-
27
- end
28
- -----------------------------------
29
-
30
- Of course, this example is anything but elaborate and doesn't improve on the default exception handling at all, but once you can catch all those exceptions you're free to do whatever you want with them. For example, you could create custom exception classes that will be thrown when a user doesn't have access to a certain section of your application:
31
-
32
- [source, ruby]
33
- -----------------------------------
34
- class ApplicationController < ActionController::Base
35
-
36
- rescue_from User::NotAuthorized, :with => :user_not_authorized
37
-
38
- private
39
-
40
- def user_not_authorized
41
- flash[:error] = "You don't have access to this section."
42
- redirect_to :back
43
- end
44
-
45
- end
46
-
47
- class ClientsController < ApplicationController
48
-
49
- # Check that the user has the right authorization to access clients.
50
- before_filter :check_authorization
51
-
52
- # Note how the actions don't have to worry about all the auth stuff.
53
- def edit
54
- @client = Client.find(params[:id])
55
- end
56
-
57
- private
58
-
59
- # If the user is not authorized, just throw the exception.
60
- def check_authorization
61
- raise User::NotAuthorized unless current_user.admin?
62
- end
63
-
64
- end
65
- -----------------------------------
66
-
67
- NOTE: Certain exceptions are only rescuable from the ApplicationController class, as they are raised before the controller gets initialized and the action gets executed. See Pratik Naik's link:http://m.onkey.org/2008/7/20/rescue-from-dispatching[article] on the subject for more information.