rails 2.1.2 → 2.2.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.
- data/CHANGELOG +27 -3
- data/Rakefile +70 -10
- data/bin/about +0 -0
- data/bin/console +0 -0
- data/bin/destroy +0 -0
- data/bin/generate +0 -0
- data/bin/performance/benchmarker +0 -0
- data/bin/performance/profiler +0 -0
- data/bin/performance/request +0 -0
- data/bin/plugin +0 -0
- data/bin/process/inspector +0 -0
- data/bin/process/reaper +0 -0
- data/bin/process/spawner +0 -0
- data/bin/runner +0 -0
- data/bin/server +0 -0
- data/config.ru +17 -0
- data/configs/apache.conf +0 -0
- data/configs/databases/ibm_db.yml +62 -0
- data/configs/databases/mysql.yml +3 -0
- data/configs/databases/postgresql.yml +3 -0
- data/configs/databases/sqlite2.yml +3 -0
- data/configs/databases/sqlite3.yml +3 -0
- data/configs/locales/en.yml +5 -0
- data/dispatches/dispatch.fcgi +1 -1
- data/dispatches/dispatch.rb +2 -2
- data/dispatches/gateway.cgi +2 -2
- data/doc/README_FOR_APP +5 -2
- data/doc/guides/html/2_2_release_notes.html +1185 -0
- data/doc/guides/html/actioncontroller_basics.html +1270 -0
- data/doc/guides/html/activerecord_validations_callbacks.html +749 -0
- data/doc/guides/html/association_basics.html +2585 -0
- data/doc/guides/html/authors.html +240 -0
- data/doc/guides/html/benchmarking_and_profiling.html +1018 -0
- data/doc/guides/html/caching_with_rails.html +583 -0
- data/doc/guides/html/command_line.html +434 -0
- data/doc/guides/html/configuring.html +438 -0
- data/doc/guides/html/creating_plugins.html +1594 -0
- data/doc/guides/html/debugging_rails_applications.html +1175 -0
- data/doc/guides/html/finders.html +1090 -0
- data/doc/guides/html/form_helpers.html +638 -0
- data/doc/guides/html/getting_started_with_rails.html +2066 -0
- data/doc/guides/html/index.html +349 -0
- data/doc/guides/html/layouts_and_rendering.html +1406 -0
- data/doc/guides/html/migrations.html +921 -0
- data/doc/guides/html/routing_outside_in.html +2213 -0
- data/doc/guides/html/security.html +1346 -0
- data/doc/guides/html/testing_rails_applications.html +1859 -0
- data/doc/guides/source/2_2_release_notes.txt +435 -0
- data/doc/guides/source/actioncontroller_basics/changelog.txt +5 -0
- data/doc/guides/source/actioncontroller_basics/cookies.txt +34 -0
- data/doc/guides/source/actioncontroller_basics/csrf.txt +32 -0
- data/doc/guides/source/actioncontroller_basics/filters.txt +119 -0
- data/doc/guides/source/actioncontroller_basics/http_auth.txt +24 -0
- data/doc/guides/source/actioncontroller_basics/index.txt +40 -0
- data/doc/guides/source/actioncontroller_basics/introduction.txt +9 -0
- data/doc/guides/source/actioncontroller_basics/methods.txt +39 -0
- data/doc/guides/source/actioncontroller_basics/parameter_filtering.txt +14 -0
- data/doc/guides/source/actioncontroller_basics/params.txt +93 -0
- data/doc/guides/source/actioncontroller_basics/request_response_objects.txt +43 -0
- data/doc/guides/source/actioncontroller_basics/rescue.txt +67 -0
- data/doc/guides/source/actioncontroller_basics/session.txt +187 -0
- data/doc/guides/source/actioncontroller_basics/streaming.txt +91 -0
- data/doc/guides/source/actioncontroller_basics/verification.txt +40 -0
- data/doc/guides/source/active_record_basics.txt +181 -0
- data/doc/guides/source/activerecord_validations_callbacks.txt +404 -0
- data/doc/guides/source/association_basics.txt +1840 -0
- data/doc/guides/source/authors.txt +39 -0
- data/doc/guides/source/benchmarking_and_profiling/appendix.txt +95 -0
- data/doc/guides/source/benchmarking_and_profiling/digging_deeper.txt +105 -0
- data/doc/guides/source/benchmarking_and_profiling/edge_rails_features.txt +185 -0
- data/doc/guides/source/benchmarking_and_profiling/gameplan.txt +27 -0
- data/doc/guides/source/benchmarking_and_profiling/index.txt +242 -0
- data/doc/guides/source/benchmarking_and_profiling/rubyprof.txt +179 -0
- data/doc/guides/source/benchmarking_and_profiling/statistics.txt +57 -0
- data/doc/guides/source/caching_with_rails.txt +367 -0
- data/doc/guides/source/command_line.txt +147 -0
- data/doc/guides/source/configuring.txt +225 -0
- data/doc/guides/source/creating_plugins/acts_as_yaffle.txt +191 -0
- data/doc/guides/source/creating_plugins/appendix.txt +46 -0
- data/doc/guides/source/creating_plugins/controllers.txt +59 -0
- data/doc/guides/source/creating_plugins/core_ext.txt +123 -0
- data/doc/guides/source/creating_plugins/custom_route.txt +69 -0
- data/doc/guides/source/creating_plugins/gem.txt +1 -0
- data/doc/guides/source/creating_plugins/generator_method.txt +89 -0
- data/doc/guides/source/creating_plugins/helpers.txt +51 -0
- data/doc/guides/source/creating_plugins/index.txt +52 -0
- data/doc/guides/source/creating_plugins/migration_generator.txt +156 -0
- data/doc/guides/source/creating_plugins/models.txt +76 -0
- data/doc/guides/source/creating_plugins/odds_and_ends.txt +69 -0
- data/doc/guides/source/creating_plugins/test_setup.txt +230 -0
- data/doc/guides/source/debugging_rails_applications.txt +733 -0
- data/doc/guides/source/finders.txt +668 -0
- data/doc/guides/source/form_helpers.txt +345 -0
- data/doc/guides/source/getting_started_with_rails.txt +1256 -0
- data/doc/guides/source/images/belongs_to.png +0 -0
- data/doc/guides/source/images/bullet.gif +0 -0
- data/doc/guides/source/images/csrf.png +0 -0
- data/doc/guides/source/images/habtm.png +0 -0
- data/doc/guides/source/images/has_many.png +0 -0
- data/doc/guides/source/images/has_many_through.png +0 -0
- data/doc/guides/source/images/has_one.png +0 -0
- data/doc/guides/source/images/has_one_through.png +0 -0
- data/doc/guides/source/images/header_backdrop.png +0 -0
- data/doc/guides/source/images/icons/README +5 -0
- data/doc/guides/source/images/icons/callouts/1.png +0 -0
- data/doc/guides/source/images/icons/callouts/10.png +0 -0
- data/doc/guides/source/images/icons/callouts/11.png +0 -0
- data/doc/guides/source/images/icons/callouts/12.png +0 -0
- data/doc/guides/source/images/icons/callouts/13.png +0 -0
- data/doc/guides/source/images/icons/callouts/14.png +0 -0
- data/doc/guides/source/images/icons/callouts/15.png +0 -0
- data/doc/guides/source/images/icons/callouts/2.png +0 -0
- data/doc/guides/source/images/icons/callouts/3.png +0 -0
- data/doc/guides/source/images/icons/callouts/4.png +0 -0
- data/doc/guides/source/images/icons/callouts/5.png +0 -0
- data/doc/guides/source/images/icons/callouts/6.png +0 -0
- data/doc/guides/source/images/icons/callouts/7.png +0 -0
- data/doc/guides/source/images/icons/callouts/8.png +0 -0
- data/doc/guides/source/images/icons/callouts/9.png +0 -0
- data/doc/guides/source/images/icons/caution.png +0 -0
- data/doc/guides/source/images/icons/example.png +0 -0
- data/doc/guides/source/images/icons/home.png +0 -0
- data/doc/guides/source/images/icons/important.png +0 -0
- data/doc/guides/source/images/icons/next.png +0 -0
- data/doc/guides/source/images/icons/note.png +0 -0
- data/doc/guides/source/images/icons/prev.png +0 -0
- data/doc/guides/source/images/icons/tip.png +0 -0
- data/doc/guides/source/images/icons/up.png +0 -0
- data/doc/guides/source/images/icons/warning.png +0 -0
- data/doc/guides/source/images/polymorphic.png +0 -0
- data/doc/guides/source/images/rails_logo_remix.gif +0 -0
- data/doc/guides/source/images/ruby_on_rails_by_mike_rundle2.gif +0 -0
- data/doc/guides/source/images/session_fixation.png +0 -0
- data/doc/guides/source/index.txt +118 -0
- data/doc/guides/source/layouts_and_rendering.txt +982 -0
- data/doc/guides/source/migrations/anatomy_of_a_migration.txt +85 -0
- data/doc/guides/source/migrations/changelog.txt +5 -0
- data/doc/guides/source/migrations/creating_a_migration.txt +109 -0
- data/doc/guides/source/migrations/foreign_keys.txt +8 -0
- data/doc/guides/source/migrations/index.txt +22 -0
- data/doc/guides/source/migrations/rakeing_around.txt +111 -0
- data/doc/guides/source/migrations/scheming.txt +47 -0
- data/doc/guides/source/migrations/using_models_in_migrations.txt +46 -0
- data/doc/guides/source/migrations/writing_a_migration.txt +159 -0
- data/doc/guides/source/routing_outside_in.txt +986 -0
- data/doc/guides/source/security.txt +984 -0
- data/doc/guides/source/stylesheets/base.css +358 -0
- data/doc/guides/source/stylesheets/forms.css +35 -0
- data/doc/guides/source/stylesheets/more.css +82 -0
- data/doc/guides/source/templates/guides.html.erb +97 -0
- data/doc/guides/source/templates/inline.css +165 -0
- data/doc/guides/source/testing_rails_applications.txt +995 -0
- data/environments/boot.rb +2 -2
- data/environments/environment.rb +9 -1
- data/environments/production.rb +3 -1
- data/helpers/performance_test.rb +9 -0
- data/html/500.html +4 -1
- data/html/javascripts/controls.js +72 -72
- data/html/javascripts/dragdrop.js +165 -164
- data/html/javascripts/effects.js +173 -165
- data/html/javascripts/prototype.js +362 -267
- data/lib/commands/console.rb +13 -0
- data/lib/commands/dbconsole.rb +2 -2
- data/lib/commands/ncgi/listener +2 -2
- data/lib/commands/ncgi/tracker +2 -2
- data/lib/commands/plugin.rb +41 -24
- data/lib/commands/process/spawner.rb +4 -4
- data/lib/commands/runner.rb +1 -1
- data/lib/commands/server.rb +11 -1
- data/lib/commands/servers/thin.rb +25 -0
- data/lib/fcgi_handler.rb +1 -3
- data/lib/initializer.rb +162 -39
- data/lib/performance_test_help.rb +5 -0
- data/lib/rails/gem_builder.rb +3 -3
- data/lib/rails/gem_dependency.rb +155 -33
- data/lib/rails/mongrel_server/commands.rb +1 -1
- data/lib/rails/plugin.rb +10 -2
- data/lib/rails/rack.rb +6 -0
- data/lib/rails/rack/logger.rb +28 -0
- data/lib/rails/rack/static.rb +35 -0
- data/lib/rails/vendor_gem_source_index.rb +140 -0
- data/lib/rails/version.rb +1 -1
- data/lib/rails_generator/commands.rb +10 -16
- data/lib/rails_generator/generated_attribute.rb +4 -0
- data/lib/rails_generator/generators/applications/app/app_generator.rb +9 -3
- data/lib/rails_generator/generators/components/controller/controller_generator.rb +1 -1
- data/lib/rails_generator/generators/components/controller/templates/functional_test.rb +1 -1
- data/lib/rails_generator/generators/components/integration_test/integration_test_generator.rb +1 -1
- data/lib/rails_generator/generators/components/integration_test/templates/integration_test.rb +2 -2
- data/lib/rails_generator/generators/components/mailer/mailer_generator.rb +1 -1
- data/lib/rails_generator/generators/components/mailer/templates/unit_test.rb +2 -3
- data/lib/rails_generator/generators/components/model/model_generator.rb +1 -1
- data/lib/rails_generator/generators/components/model/templates/model.rb +3 -0
- data/lib/rails_generator/generators/components/model/templates/unit_test.rb +1 -1
- data/lib/rails_generator/generators/components/observer/observer_generator.rb +1 -1
- data/lib/rails_generator/generators/components/observer/templates/unit_test.rb +1 -1
- data/lib/rails_generator/generators/components/performance_test/USAGE +8 -0
- data/lib/rails_generator/generators/components/performance_test/performance_test_generator.rb +16 -0
- data/lib/rails_generator/generators/components/performance_test/templates/performance_test.rb +9 -0
- data/lib/rails_generator/generators/components/plugin/plugin_generator.rb +11 -11
- data/lib/rails_generator/generators/components/plugin/templates/Rakefile +1 -0
- data/lib/rails_generator/generators/components/plugin/templates/test_helper.rb +3 -0
- data/lib/rails_generator/generators/components/plugin/templates/unit_test.rb +4 -4
- data/lib/rails_generator/generators/components/resource/resource_generator.rb +2 -2
- data/lib/rails_generator/generators/components/resource/templates/functional_test.rb +1 -1
- data/lib/rails_generator/generators/components/scaffold/USAGE +9 -5
- data/lib/rails_generator/generators/components/scaffold/scaffold_generator.rb +10 -3
- data/lib/rails_generator/generators/components/scaffold/templates/functional_test.rb +7 -7
- data/lib/rails_generator/scripts.rb +1 -1
- data/lib/rails_generator/scripts/destroy.rb +6 -7
- data/lib/rails_generator/secret_key_generator.rb +5 -147
- data/lib/tasks/annotations.rake +8 -11
- data/lib/tasks/databases.rake +42 -15
- data/lib/tasks/documentation.rake +15 -0
- data/lib/tasks/framework.rake +5 -1
- data/lib/tasks/gems.rake +24 -8
- data/lib/tasks/misc.rake +3 -4
- data/lib/tasks/testing.rake +25 -4
- data/lib/test_help.rb +1 -2
- data/lib/webrick_server.rb +2 -11
- metadata +165 -9
@@ -0,0 +1,32 @@
|
|
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.
|
@@ -0,0 +1,119 @@
|
|
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].
|
@@ -0,0 +1,24 @@
|
|
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.
|
@@ -0,0 +1,40 @@
|
|
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[]
|
@@ -0,0 +1,9 @@
|
|
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].
|
@@ -0,0 +1,39 @@
|
|
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.
|
@@ -0,0 +1,14 @@
|
|
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.
|
@@ -0,0 +1,93 @@
|
|
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.
|
@@ -0,0 +1,43 @@
|
|
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
|
+
-------------------------------------
|
@@ -0,0 +1,67 @@
|
|
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.
|