actionpack 3.2.22.5 → 4.0.0.beta1
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of actionpack might be problematic. Click here for more details.
- checksums.yaml +4 -4
- data/CHANGELOG.md +641 -418
- data/MIT-LICENSE +1 -1
- data/README.rdoc +5 -288
- data/lib/abstract_controller.rb +1 -8
- data/lib/abstract_controller/asset_paths.rb +2 -2
- data/lib/abstract_controller/base.rb +39 -37
- data/lib/abstract_controller/callbacks.rb +101 -82
- data/lib/abstract_controller/collector.rb +7 -3
- data/lib/abstract_controller/helpers.rb +23 -11
- data/lib/abstract_controller/layouts.rb +68 -73
- data/lib/abstract_controller/logger.rb +1 -2
- data/lib/abstract_controller/rendering.rb +22 -13
- data/lib/abstract_controller/translation.rb +16 -1
- data/lib/abstract_controller/url_for.rb +6 -6
- data/lib/abstract_controller/view_paths.rb +1 -1
- data/lib/action_controller.rb +15 -6
- data/lib/action_controller/base.rb +46 -22
- data/lib/action_controller/caching.rb +46 -33
- data/lib/action_controller/caching/fragments.rb +23 -53
- data/lib/action_controller/deprecated.rb +5 -1
- data/lib/action_controller/deprecated/integration_test.rb +3 -0
- data/lib/action_controller/log_subscriber.rb +11 -8
- data/lib/action_controller/metal.rb +16 -30
- data/lib/action_controller/metal/conditional_get.rb +76 -32
- data/lib/action_controller/metal/data_streaming.rb +20 -26
- data/lib/action_controller/metal/exceptions.rb +19 -6
- data/lib/action_controller/metal/flash.rb +24 -9
- data/lib/action_controller/metal/force_ssl.rb +32 -9
- data/lib/action_controller/metal/head.rb +25 -4
- data/lib/action_controller/metal/helpers.rb +6 -9
- data/lib/action_controller/metal/hide_actions.rb +1 -2
- data/lib/action_controller/metal/http_authentication.rb +105 -87
- data/lib/action_controller/metal/implicit_render.rb +1 -1
- data/lib/action_controller/metal/instrumentation.rb +2 -1
- data/lib/action_controller/metal/live.rb +141 -0
- data/lib/action_controller/metal/mime_responds.rb +161 -47
- data/lib/action_controller/metal/params_wrapper.rb +112 -74
- data/lib/action_controller/metal/rack_delegation.rb +9 -3
- data/lib/action_controller/metal/redirecting.rb +15 -20
- data/lib/action_controller/metal/renderers.rb +11 -9
- data/lib/action_controller/metal/rendering.rb +8 -0
- data/lib/action_controller/metal/request_forgery_protection.rb +112 -19
- data/lib/action_controller/metal/responder.rb +20 -19
- data/lib/action_controller/metal/streaming.rb +12 -18
- data/lib/action_controller/metal/strong_parameters.rb +516 -0
- data/lib/action_controller/metal/testing.rb +13 -18
- data/lib/action_controller/metal/url_for.rb +27 -25
- data/lib/action_controller/model_naming.rb +12 -0
- data/lib/action_controller/railtie.rb +33 -17
- data/lib/action_controller/railties/helpers.rb +22 -0
- data/lib/action_controller/record_identifier.rb +18 -72
- data/lib/action_controller/test_case.rb +215 -123
- data/lib/action_controller/vendor/html-scanner.rb +4 -19
- data/lib/action_dispatch.rb +27 -19
- data/lib/action_dispatch/http/cache.rb +63 -11
- data/lib/action_dispatch/http/filter_parameters.rb +18 -8
- data/lib/action_dispatch/http/filter_redirect.rb +37 -0
- data/lib/action_dispatch/http/headers.rb +27 -19
- data/lib/action_dispatch/http/mime_negotiation.rb +25 -2
- data/lib/action_dispatch/http/mime_type.rb +145 -113
- data/lib/action_dispatch/http/mime_types.rb +1 -1
- data/lib/action_dispatch/http/parameter_filter.rb +44 -46
- data/lib/action_dispatch/http/parameters.rb +12 -5
- data/lib/action_dispatch/http/rack_cache.rb +2 -3
- data/lib/action_dispatch/http/request.rb +49 -18
- data/lib/action_dispatch/http/response.rb +129 -35
- data/lib/action_dispatch/http/upload.rb +60 -17
- data/lib/action_dispatch/http/url.rb +53 -31
- data/lib/action_dispatch/journey.rb +5 -0
- data/lib/action_dispatch/journey/backwards.rb +5 -0
- data/lib/action_dispatch/journey/formatter.rb +146 -0
- data/lib/action_dispatch/journey/gtg/builder.rb +162 -0
- data/lib/action_dispatch/journey/gtg/simulator.rb +44 -0
- data/lib/action_dispatch/journey/gtg/transition_table.rb +156 -0
- data/lib/action_dispatch/journey/nfa/builder.rb +76 -0
- data/lib/action_dispatch/journey/nfa/dot.rb +36 -0
- data/lib/action_dispatch/journey/nfa/simulator.rb +47 -0
- data/lib/action_dispatch/journey/nfa/transition_table.rb +163 -0
- data/lib/action_dispatch/journey/nodes/node.rb +124 -0
- data/lib/action_dispatch/journey/parser.rb +206 -0
- data/lib/action_dispatch/journey/parser.y +47 -0
- data/lib/action_dispatch/journey/parser_extras.rb +23 -0
- data/lib/action_dispatch/journey/path/pattern.rb +196 -0
- data/lib/action_dispatch/journey/route.rb +116 -0
- data/lib/action_dispatch/journey/router.rb +164 -0
- data/lib/action_dispatch/journey/router/strexp.rb +24 -0
- data/lib/action_dispatch/journey/router/utils.rb +54 -0
- data/lib/action_dispatch/journey/routes.rb +75 -0
- data/lib/action_dispatch/journey/scanner.rb +61 -0
- data/lib/action_dispatch/journey/visitors.rb +189 -0
- data/lib/action_dispatch/journey/visualizer/fsm.css +34 -0
- data/lib/action_dispatch/journey/visualizer/fsm.js +134 -0
- data/lib/action_dispatch/journey/visualizer/index.html.erb +52 -0
- data/lib/action_dispatch/middleware/callbacks.rb +9 -4
- data/lib/action_dispatch/middleware/cookies.rb +168 -57
- data/lib/action_dispatch/middleware/debug_exceptions.rb +26 -17
- data/lib/action_dispatch/middleware/exception_wrapper.rb +27 -3
- data/lib/action_dispatch/middleware/flash.rb +58 -58
- data/lib/action_dispatch/middleware/params_parser.rb +14 -29
- data/lib/action_dispatch/middleware/public_exceptions.rb +31 -14
- data/lib/action_dispatch/middleware/reloader.rb +6 -6
- data/lib/action_dispatch/middleware/remote_ip.rb +145 -39
- data/lib/action_dispatch/middleware/request_id.rb +2 -6
- data/lib/action_dispatch/middleware/session/abstract_store.rb +22 -20
- data/lib/action_dispatch/middleware/session/cache_store.rb +3 -3
- data/lib/action_dispatch/middleware/session/cookie_store.rb +81 -7
- data/lib/action_dispatch/middleware/session/mem_cache_store.rb +8 -3
- data/lib/action_dispatch/middleware/show_exceptions.rb +12 -45
- data/lib/action_dispatch/middleware/ssl.rb +70 -0
- data/lib/action_dispatch/middleware/stack.rb +6 -1
- data/lib/action_dispatch/middleware/static.rb +5 -24
- data/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb +14 -11
- data/lib/action_dispatch/middleware/templates/rescues/_source.erb +25 -0
- data/lib/action_dispatch/middleware/templates/rescues/_trace.erb +3 -3
- data/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb +15 -9
- data/lib/action_dispatch/middleware/templates/rescues/layout.erb +121 -5
- data/lib/action_dispatch/middleware/templates/rescues/missing_template.erb +7 -2
- data/lib/action_dispatch/middleware/templates/rescues/routing_error.erb +30 -15
- data/lib/action_dispatch/middleware/templates/rescues/template_error.erb +39 -13
- data/lib/action_dispatch/middleware/templates/rescues/unknown_action.erb +6 -2
- data/lib/action_dispatch/middleware/templates/routes/_route.html.erb +16 -0
- data/lib/action_dispatch/middleware/templates/routes/_table.html.erb +144 -0
- data/lib/action_dispatch/railtie.rb +16 -6
- data/lib/action_dispatch/request/session.rb +181 -0
- data/lib/action_dispatch/routing.rb +41 -40
- data/lib/action_dispatch/routing/inspector.rb +240 -0
- data/lib/action_dispatch/routing/mapper.rb +501 -273
- data/lib/action_dispatch/routing/polymorphic_routes.rb +16 -20
- data/lib/action_dispatch/routing/redirection.rb +46 -29
- data/lib/action_dispatch/routing/route_set.rb +203 -164
- data/lib/action_dispatch/routing/routes_proxy.rb +2 -0
- data/lib/action_dispatch/routing/url_for.rb +48 -33
- data/lib/action_dispatch/testing/assertions/dom.rb +3 -13
- data/lib/action_dispatch/testing/assertions/response.rb +32 -40
- data/lib/action_dispatch/testing/assertions/routing.rb +40 -39
- data/lib/action_dispatch/testing/assertions/selector.rb +15 -20
- data/lib/action_dispatch/testing/assertions/tag.rb +20 -23
- data/lib/action_dispatch/testing/integration.rb +41 -22
- data/lib/action_dispatch/testing/test_process.rb +9 -6
- data/lib/action_dispatch/testing/test_request.rb +7 -3
- data/lib/action_pack.rb +1 -1
- data/lib/action_pack/version.rb +4 -4
- data/lib/action_view.rb +17 -8
- data/lib/action_view/base.rb +15 -34
- data/lib/action_view/buffers.rb +1 -1
- data/lib/action_view/context.rb +4 -4
- data/lib/action_view/dependency_tracker.rb +91 -0
- data/lib/action_view/digestor.rb +85 -0
- data/lib/action_view/flows.rb +1 -4
- data/lib/action_view/helpers.rb +2 -4
- data/lib/action_view/helpers/active_model_helper.rb +3 -4
- data/lib/action_view/helpers/asset_tag_helper.rb +211 -353
- data/lib/action_view/helpers/asset_url_helper.rb +354 -0
- data/lib/action_view/helpers/atom_feed_helper.rb +13 -10
- data/lib/action_view/helpers/cache_helper.rb +150 -18
- data/lib/action_view/helpers/capture_helper.rb +42 -29
- data/lib/action_view/helpers/csrf_helper.rb +0 -2
- data/lib/action_view/helpers/date_helper.rb +268 -247
- data/lib/action_view/helpers/debug_helper.rb +10 -11
- data/lib/action_view/helpers/form_helper.rb +904 -547
- data/lib/action_view/helpers/form_options_helper.rb +341 -166
- data/lib/action_view/helpers/form_tag_helper.rb +188 -88
- data/lib/action_view/helpers/javascript_helper.rb +23 -16
- data/lib/action_view/helpers/number_helper.rb +148 -354
- data/lib/action_view/helpers/output_safety_helper.rb +3 -3
- data/lib/action_view/helpers/record_tag_helper.rb +17 -22
- data/lib/action_view/helpers/rendering_helper.rb +2 -4
- data/lib/action_view/helpers/sanitize_helper.rb +3 -6
- data/lib/action_view/helpers/tag_helper.rb +43 -37
- data/lib/action_view/helpers/tags.rb +39 -0
- data/lib/action_view/helpers/tags/base.rb +148 -0
- data/lib/action_view/helpers/tags/check_box.rb +64 -0
- data/lib/action_view/helpers/tags/checkable.rb +16 -0
- data/lib/action_view/helpers/tags/collection_check_boxes.rb +43 -0
- data/lib/action_view/helpers/tags/collection_helpers.rb +83 -0
- data/lib/action_view/helpers/tags/collection_radio_buttons.rb +36 -0
- data/lib/action_view/helpers/tags/collection_select.rb +28 -0
- data/lib/action_view/helpers/tags/color_field.rb +25 -0
- data/lib/action_view/helpers/tags/date_field.rb +13 -0
- data/lib/action_view/helpers/tags/date_select.rb +72 -0
- data/lib/action_view/helpers/tags/datetime_field.rb +22 -0
- data/lib/action_view/helpers/tags/datetime_local_field.rb +19 -0
- data/lib/action_view/helpers/tags/datetime_select.rb +8 -0
- data/lib/action_view/helpers/tags/email_field.rb +8 -0
- data/lib/action_view/helpers/tags/file_field.rb +8 -0
- data/lib/action_view/helpers/tags/grouped_collection_select.rb +29 -0
- data/lib/action_view/helpers/tags/hidden_field.rb +8 -0
- data/lib/action_view/helpers/tags/label.rb +65 -0
- data/lib/action_view/helpers/tags/month_field.rb +13 -0
- data/lib/action_view/helpers/tags/number_field.rb +18 -0
- data/lib/action_view/helpers/tags/password_field.rb +12 -0
- data/lib/action_view/helpers/tags/radio_button.rb +31 -0
- data/lib/action_view/helpers/tags/range_field.rb +8 -0
- data/lib/action_view/helpers/tags/search_field.rb +24 -0
- data/lib/action_view/helpers/tags/select.rb +41 -0
- data/lib/action_view/helpers/tags/tel_field.rb +8 -0
- data/lib/action_view/helpers/tags/text_area.rb +18 -0
- data/lib/action_view/helpers/tags/text_field.rb +29 -0
- data/lib/action_view/helpers/tags/time_field.rb +13 -0
- data/lib/action_view/helpers/tags/time_select.rb +8 -0
- data/lib/action_view/helpers/tags/time_zone_select.rb +20 -0
- data/lib/action_view/helpers/tags/url_field.rb +8 -0
- data/lib/action_view/helpers/tags/week_field.rb +13 -0
- data/lib/action_view/helpers/text_helper.rb +126 -113
- data/lib/action_view/helpers/translation_helper.rb +32 -16
- data/lib/action_view/helpers/url_helper.rb +200 -271
- data/lib/action_view/locale/en.yml +1 -105
- data/lib/action_view/log_subscriber.rb +6 -4
- data/lib/action_view/lookup_context.rb +15 -39
- data/lib/action_view/model_naming.rb +12 -0
- data/lib/action_view/path_set.rb +9 -39
- data/lib/action_view/railtie.rb +6 -22
- data/lib/action_view/record_identifier.rb +84 -0
- data/lib/action_view/renderer/abstract_renderer.rb +10 -19
- data/lib/action_view/renderer/partial_renderer.rb +144 -81
- data/lib/action_view/renderer/renderer.rb +2 -19
- data/lib/action_view/renderer/streaming_template_renderer.rb +2 -5
- data/lib/action_view/renderer/template_renderer.rb +14 -13
- data/lib/action_view/routing_url_for.rb +107 -0
- data/lib/action_view/template.rb +22 -21
- data/lib/action_view/template/error.rb +22 -12
- data/lib/action_view/template/handlers.rb +12 -9
- data/lib/action_view/template/handlers/builder.rb +1 -1
- data/lib/action_view/template/handlers/erb.rb +11 -16
- data/lib/action_view/template/handlers/raw.rb +11 -0
- data/lib/action_view/template/resolver.rb +111 -83
- data/lib/action_view/template/text.rb +12 -8
- data/lib/action_view/template/types.rb +57 -0
- data/lib/action_view/test_case.rb +66 -43
- data/lib/action_view/testing/resolvers.rb +3 -2
- data/lib/action_view/vendor/html-scanner.rb +20 -0
- data/lib/{action_controller → action_view}/vendor/html-scanner/html/document.rb +0 -0
- data/lib/{action_controller → action_view}/vendor/html-scanner/html/node.rb +12 -12
- data/lib/{action_controller → action_view}/vendor/html-scanner/html/sanitizer.rb +18 -7
- data/lib/{action_controller → action_view}/vendor/html-scanner/html/selector.rb +1 -1
- data/lib/{action_controller → action_view}/vendor/html-scanner/html/tokenizer.rb +1 -1
- data/lib/{action_controller → action_view}/vendor/html-scanner/html/version.rb +0 -0
- metadata +135 -125
- data/lib/action_controller/caching/actions.rb +0 -185
- data/lib/action_controller/caching/pages.rb +0 -187
- data/lib/action_controller/caching/sweeping.rb +0 -97
- data/lib/action_controller/deprecated/performance_test.rb +0 -1
- data/lib/action_controller/metal/compatibility.rb +0 -65
- data/lib/action_controller/metal/session_management.rb +0 -14
- data/lib/action_controller/railties/paths.rb +0 -25
- data/lib/action_dispatch/middleware/best_standards_support.rb +0 -30
- data/lib/action_dispatch/middleware/body_proxy.rb +0 -30
- data/lib/action_dispatch/middleware/head.rb +0 -18
- data/lib/action_dispatch/middleware/rescue.rb +0 -26
- data/lib/action_dispatch/testing/performance_test.rb +0 -10
- data/lib/action_view/asset_paths.rb +0 -142
- data/lib/action_view/helpers/asset_paths.rb +0 -7
- data/lib/action_view/helpers/asset_tag_helpers/asset_include_tag.rb +0 -146
- data/lib/action_view/helpers/asset_tag_helpers/asset_paths.rb +0 -93
- data/lib/action_view/helpers/asset_tag_helpers/javascript_tag_helpers.rb +0 -193
- data/lib/action_view/helpers/asset_tag_helpers/stylesheet_tag_helpers.rb +0 -148
- data/lib/sprockets/assets.rake +0 -99
- data/lib/sprockets/bootstrap.rb +0 -37
- data/lib/sprockets/compressors.rb +0 -83
- data/lib/sprockets/helpers.rb +0 -6
- data/lib/sprockets/helpers/isolated_helper.rb +0 -13
- data/lib/sprockets/helpers/rails_helper.rb +0 -182
- data/lib/sprockets/railtie.rb +0 -62
- data/lib/sprockets/static_compiler.rb +0 -56
@@ -45,15 +45,15 @@ module ActionController #:nodoc:
|
|
45
45
|
# if @user.save
|
46
46
|
# flash[:notice] = 'User was successfully created.'
|
47
47
|
# format.html { redirect_to(@user) }
|
48
|
-
# format.xml { render :
|
48
|
+
# format.xml { render xml: @user, status: :created, location: @user }
|
49
49
|
# else
|
50
|
-
# format.html { render :
|
51
|
-
# format.xml { render :
|
50
|
+
# format.html { render action: "new" }
|
51
|
+
# format.xml { render xml: @user.errors, status: :unprocessable_entity }
|
52
52
|
# end
|
53
53
|
# end
|
54
54
|
# end
|
55
55
|
#
|
56
|
-
# The same happens for PUT and DELETE requests.
|
56
|
+
# The same happens for PATCH/PUT and DELETE requests.
|
57
57
|
#
|
58
58
|
# === Nested resources
|
59
59
|
#
|
@@ -63,7 +63,7 @@ module ActionController #:nodoc:
|
|
63
63
|
#
|
64
64
|
# def create
|
65
65
|
# @project = Project.find(params[:project_id])
|
66
|
-
# @task = @project.
|
66
|
+
# @task = @project.tasks.build(params[:task])
|
67
67
|
# flash[:notice] = 'Task was successfully created.' if @task.save
|
68
68
|
# respond_with(@project, @task)
|
69
69
|
# end
|
@@ -90,9 +90,9 @@ module ActionController #:nodoc:
|
|
90
90
|
#
|
91
91
|
# def create
|
92
92
|
# @project = Project.find(params[:project_id])
|
93
|
-
# @task = @project.
|
93
|
+
# @task = @project.tasks.build(params[:task])
|
94
94
|
# flash[:notice] = 'Task was successfully created.' if @task.save
|
95
|
-
# respond_with(@project, @task, :
|
95
|
+
# respond_with(@project, @task, status: 201)
|
96
96
|
# end
|
97
97
|
#
|
98
98
|
# This will return status 201 if the task was saved successfully. If not,
|
@@ -102,8 +102,8 @@ module ActionController #:nodoc:
|
|
102
102
|
#
|
103
103
|
# def create
|
104
104
|
# @project = Project.find(params[:project_id])
|
105
|
-
# @task = @project.
|
106
|
-
# respond_with(@project, @task, :
|
105
|
+
# @task = @project.tasks.build(params[:task])
|
106
|
+
# respond_with(@project, @task, status: 201) do |format|
|
107
107
|
# if @task.save
|
108
108
|
# flash[:notice] = 'Task was successfully created.'
|
109
109
|
# else
|
@@ -116,8 +116,9 @@ module ActionController #:nodoc:
|
|
116
116
|
class Responder
|
117
117
|
attr_reader :controller, :request, :format, :resource, :resources, :options
|
118
118
|
|
119
|
-
|
119
|
+
DEFAULT_ACTIONS_FOR_VERBS = {
|
120
120
|
:post => :new,
|
121
|
+
:patch => :edit,
|
121
122
|
:put => :edit
|
122
123
|
}
|
123
124
|
|
@@ -133,7 +134,7 @@ module ActionController #:nodoc:
|
|
133
134
|
end
|
134
135
|
|
135
136
|
delegate :head, :render, :redirect_to, :to => :controller
|
136
|
-
delegate :get?, :post?, :put?, :delete?, :to => :request
|
137
|
+
delegate :get?, :post?, :patch?, :put?, :delete?, :to => :request
|
137
138
|
|
138
139
|
# Undefine :to_json and :to_yaml since it's defined on Object
|
139
140
|
undef_method(:to_json) if method_defined?(:to_json)
|
@@ -235,20 +236,20 @@ module ActionController #:nodoc:
|
|
235
236
|
|
236
237
|
# Display is just a shortcut to render a resource with the current format.
|
237
238
|
#
|
238
|
-
# display @user, :
|
239
|
+
# display @user, status: :ok
|
239
240
|
#
|
240
241
|
# For XML requests it's equivalent to:
|
241
242
|
#
|
242
|
-
# render :
|
243
|
+
# render xml: @user, status: :ok
|
243
244
|
#
|
244
245
|
# Options sent by the user are also used:
|
245
246
|
#
|
246
|
-
# respond_with(@user, :
|
247
|
-
# display(@user, :
|
247
|
+
# respond_with(@user, status: :created)
|
248
|
+
# display(@user, status: :ok)
|
248
249
|
#
|
249
250
|
# Results in:
|
250
251
|
#
|
251
|
-
# render :
|
252
|
+
# render xml: @user, status: :created
|
252
253
|
#
|
253
254
|
def display(resource, given_options={})
|
254
255
|
controller.render given_options.merge!(options).merge!(format => resource)
|
@@ -264,11 +265,11 @@ module ActionController #:nodoc:
|
|
264
265
|
resource.respond_to?(:errors) && !resource.errors.empty?
|
265
266
|
end
|
266
267
|
|
267
|
-
# By default, render the <code>:edit</code> action for HTML requests with
|
268
|
-
# the verb
|
268
|
+
# By default, render the <code>:edit</code> action for HTML requests with errors, unless
|
269
|
+
# the verb was POST.
|
269
270
|
#
|
270
271
|
def default_action
|
271
|
-
@action ||=
|
272
|
+
@action ||= DEFAULT_ACTIONS_FOR_VERBS[request.request_method_symbol]
|
272
273
|
end
|
273
274
|
|
274
275
|
def resource_errors
|
@@ -1,4 +1,3 @@
|
|
1
|
-
require 'active_support/core_ext/file/path'
|
2
1
|
require 'rack/chunked'
|
3
2
|
|
4
3
|
module ActionController #:nodoc:
|
@@ -22,15 +21,13 @@ module ActionController #:nodoc:
|
|
22
21
|
# supports fibers (fibers are supported since version 1.9.2 of the main
|
23
22
|
# Ruby implementation).
|
24
23
|
#
|
25
|
-
# == Examples
|
26
|
-
#
|
27
24
|
# Streaming can be added to a given template easily, all you need to do is
|
28
25
|
# to pass the :stream option.
|
29
26
|
#
|
30
27
|
# class PostsController
|
31
28
|
# def index
|
32
|
-
# @posts = Post.
|
33
|
-
# render :
|
29
|
+
# @posts = Post.all
|
30
|
+
# render stream: true
|
34
31
|
# end
|
35
32
|
# end
|
36
33
|
#
|
@@ -54,10 +51,10 @@ module ActionController #:nodoc:
|
|
54
51
|
#
|
55
52
|
# def dashboard
|
56
53
|
# # Allow lazy execution of the queries
|
57
|
-
# @posts = Post.
|
58
|
-
# @pages = Page.
|
59
|
-
# @articles = Article.
|
60
|
-
# render :
|
54
|
+
# @posts = Post.all
|
55
|
+
# @pages = Page.all
|
56
|
+
# @articles = Article.all
|
57
|
+
# render stream: true
|
61
58
|
# end
|
62
59
|
#
|
63
60
|
# Notice that :stream only works with templates. Rendering :json
|
@@ -140,17 +137,14 @@ module ActionController #:nodoc:
|
|
140
137
|
# session or flash after the template starts rendering will not propagate
|
141
138
|
# to the client.
|
142
139
|
#
|
143
|
-
# If you try to modify cookies, session or flash, an +ActionDispatch::ClosedError+
|
144
|
-
# will be raised, showing those objects are closed for modification.
|
145
|
-
#
|
146
140
|
# == Middlewares
|
147
141
|
#
|
148
142
|
# Middlewares that need to manipulate the body won't work with streaming.
|
149
143
|
# You should disable those middlewares whenever streaming in development
|
150
|
-
# or production. For instance,
|
144
|
+
# or production. For instance, <tt>Rack::Bug</tt> won't work when streaming as it
|
151
145
|
# needs to inject contents in the HTML body.
|
152
146
|
#
|
153
|
-
# Also
|
147
|
+
# Also <tt>Rack::Cache</tt> won't work with streaming as it does not support
|
154
148
|
# streaming bodies yet. Whenever streaming Cache-Control is automatically
|
155
149
|
# set to "no-cache".
|
156
150
|
#
|
@@ -163,7 +157,7 @@ module ActionController #:nodoc:
|
|
163
157
|
# Currently, when an exception happens in development or production, Rails
|
164
158
|
# will automatically stream to the client:
|
165
159
|
#
|
166
|
-
# "><script
|
160
|
+
# "><script>window.location = "/500.html"</script></html>
|
167
161
|
#
|
168
162
|
# The first two characters (">) are required in case the exception happens
|
169
163
|
# while rendering attributes for a given tag. You can check the real cause
|
@@ -180,7 +174,7 @@ module ActionController #:nodoc:
|
|
180
174
|
# need to create a config file as follow:
|
181
175
|
#
|
182
176
|
# # unicorn.config.rb
|
183
|
-
# listen 3000, :
|
177
|
+
# listen 3000, tcp_nopush: false
|
184
178
|
#
|
185
179
|
# And use it on initialization:
|
186
180
|
#
|
@@ -195,7 +189,7 @@ module ActionController #:nodoc:
|
|
195
189
|
# ==== Passenger
|
196
190
|
#
|
197
191
|
# To be described.
|
198
|
-
#
|
192
|
+
#
|
199
193
|
module Streaming
|
200
194
|
extend ActiveSupport::Concern
|
201
195
|
|
@@ -217,7 +211,7 @@ module ActionController #:nodoc:
|
|
217
211
|
end
|
218
212
|
end
|
219
213
|
|
220
|
-
# Call
|
214
|
+
# Call render_body if we are streaming instead of usual +render+.
|
221
215
|
def _render_template(options) #:nodoc:
|
222
216
|
if options.delete(:stream)
|
223
217
|
Rack::Chunked::Body.new view_renderer.render_body(view_context, options)
|
@@ -0,0 +1,516 @@
|
|
1
|
+
require 'active_support/core_ext/hash/indifferent_access'
|
2
|
+
require 'active_support/core_ext/array/wrap'
|
3
|
+
require 'active_support/rescuable'
|
4
|
+
require 'action_dispatch/http/upload'
|
5
|
+
|
6
|
+
module ActionController
|
7
|
+
# Raised when a required parameter is missing.
|
8
|
+
#
|
9
|
+
# params = ActionController::Parameters.new(a: {})
|
10
|
+
# params.fetch(:b)
|
11
|
+
# # => ActionController::ParameterMissing: param not found: b
|
12
|
+
# params.require(:a)
|
13
|
+
# # => ActionController::ParameterMissing: param not found: a
|
14
|
+
class ParameterMissing < KeyError
|
15
|
+
attr_reader :param # :nodoc:
|
16
|
+
|
17
|
+
def initialize(param) # :nodoc:
|
18
|
+
@param = param
|
19
|
+
super("param not found: #{param}")
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
# Raised when a supplied parameter is not expected.
|
24
|
+
#
|
25
|
+
# params = ActionController::Parameters.new(a: "123", b: "456")
|
26
|
+
# params.permit(:c)
|
27
|
+
# # => ActionController::UnpermittedParameters: found unexpected keys: a, b
|
28
|
+
class UnpermittedParameters < IndexError
|
29
|
+
attr_reader :params # :nodoc:
|
30
|
+
|
31
|
+
def initialize(params) # :nodoc:
|
32
|
+
@params = params
|
33
|
+
super("found unpermitted parameters: #{params.join(", ")}")
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
# == Action Controller \Parameters
|
38
|
+
#
|
39
|
+
# Allows to choose which attributes should be whitelisted for mass updating
|
40
|
+
# and thus prevent accidentally exposing that which shouldn’t be exposed.
|
41
|
+
# Provides two methods for this purpose: #require and #permit. The former is
|
42
|
+
# used to mark parameters as required. The latter is used to set the parameter
|
43
|
+
# as permitted and limit which attributes should be allowed for mass updating.
|
44
|
+
#
|
45
|
+
# params = ActionController::Parameters.new({
|
46
|
+
# person: {
|
47
|
+
# name: 'Francesco',
|
48
|
+
# age: 22,
|
49
|
+
# role: 'admin'
|
50
|
+
# }
|
51
|
+
# })
|
52
|
+
#
|
53
|
+
# permitted = params.require(:person).permit(:name, :age)
|
54
|
+
# permitted # => {"name"=>"Francesco", "age"=>22}
|
55
|
+
# permitted.class # => ActionController::Parameters
|
56
|
+
# permitted.permitted? # => true
|
57
|
+
#
|
58
|
+
# Person.first.update!(permitted)
|
59
|
+
# # => #<Person id: 1, name: "Francesco", age: 22, role: "user">
|
60
|
+
#
|
61
|
+
# It provides two options that controls the top-level behavior of new instances:
|
62
|
+
#
|
63
|
+
# * +permit_all_parameters+ - If it's +true+, all the parameters will be
|
64
|
+
# permitted by default. The default is +false+.
|
65
|
+
# * +action_on_unpermitted_parameters+ - Allow to control the behavior when parameters
|
66
|
+
# that are not explicitly permitted are found. The values can be <tt>:log</tt> to
|
67
|
+
# write a message on the logger or <tt>:raise</tt> to raise
|
68
|
+
# ActionController::UnpermittedParameters exception. The default value is <tt>:log</tt>
|
69
|
+
# in test and development environments, +false+ otherwise.
|
70
|
+
#
|
71
|
+
# params = ActionController::Parameters.new
|
72
|
+
# params.permitted? # => false
|
73
|
+
#
|
74
|
+
# ActionController::Parameters.permit_all_parameters = true
|
75
|
+
#
|
76
|
+
# params = ActionController::Parameters.new
|
77
|
+
# params.permitted? # => true
|
78
|
+
#
|
79
|
+
# params = ActionController::Parameters.new(a: "123", b: "456")
|
80
|
+
# params.permit(:c)
|
81
|
+
# # => {}
|
82
|
+
#
|
83
|
+
# ActionController::Parameters.action_on_unpermitted_parameters = :raise
|
84
|
+
#
|
85
|
+
# params = ActionController::Parameters.new(a: "123", b: "456")
|
86
|
+
# params.permit(:c)
|
87
|
+
# # => ActionController::UnpermittedParameters: found unpermitted keys: a, b
|
88
|
+
#
|
89
|
+
# <tt>ActionController::Parameters</tt> is inherited from
|
90
|
+
# <tt>ActiveSupport::HashWithIndifferentAccess</tt>, this means
|
91
|
+
# that you can fetch values using either <tt>:key</tt> or <tt>"key"</tt>.
|
92
|
+
#
|
93
|
+
# params = ActionController::Parameters.new(key: 'value')
|
94
|
+
# params[:key] # => "value"
|
95
|
+
# params["key"] # => "value"
|
96
|
+
class Parameters < ActiveSupport::HashWithIndifferentAccess
|
97
|
+
cattr_accessor :permit_all_parameters, instance_accessor: false
|
98
|
+
cattr_accessor :action_on_unpermitted_parameters, instance_accessor: false
|
99
|
+
|
100
|
+
# Never raise an UnpermittedParameters exception because of these params
|
101
|
+
# are present. They are added by Rails and it's of no concern.
|
102
|
+
NEVER_UNPERMITTED_PARAMS = %w( controller action )
|
103
|
+
|
104
|
+
# Returns a new instance of <tt>ActionController::Parameters</tt>.
|
105
|
+
# Also, sets the +permitted+ attribute to the default value of
|
106
|
+
# <tt>ActionController::Parameters.permit_all_parameters</tt>.
|
107
|
+
#
|
108
|
+
# class Person < ActiveRecord::Base
|
109
|
+
# end
|
110
|
+
#
|
111
|
+
# params = ActionController::Parameters.new(name: 'Francesco')
|
112
|
+
# params.permitted? # => false
|
113
|
+
# Person.new(params) # => ActiveModel::ForbiddenAttributesError
|
114
|
+
#
|
115
|
+
# ActionController::Parameters.permit_all_parameters = true
|
116
|
+
#
|
117
|
+
# params = ActionController::Parameters.new(name: 'Francesco')
|
118
|
+
# params.permitted? # => true
|
119
|
+
# Person.new(params) # => #<Person id: nil, name: "Francesco">
|
120
|
+
def initialize(attributes = nil)
|
121
|
+
super(attributes)
|
122
|
+
@permitted = self.class.permit_all_parameters
|
123
|
+
end
|
124
|
+
|
125
|
+
# Returns +true+ if the parameter is permitted, +false+ otherwise.
|
126
|
+
#
|
127
|
+
# params = ActionController::Parameters.new
|
128
|
+
# params.permitted? # => false
|
129
|
+
# params.permit!
|
130
|
+
# params.permitted? # => true
|
131
|
+
def permitted?
|
132
|
+
@permitted
|
133
|
+
end
|
134
|
+
|
135
|
+
# Sets the +permitted+ attribute to +true+. This can be used to pass
|
136
|
+
# mass assignment. Returns +self+.
|
137
|
+
#
|
138
|
+
# class Person < ActiveRecord::Base
|
139
|
+
# end
|
140
|
+
#
|
141
|
+
# params = ActionController::Parameters.new(name: 'Francesco')
|
142
|
+
# params.permitted? # => false
|
143
|
+
# Person.new(params) # => ActiveModel::ForbiddenAttributesError
|
144
|
+
# params.permit!
|
145
|
+
# params.permitted? # => true
|
146
|
+
# Person.new(params) # => #<Person id: nil, name: "Francesco">
|
147
|
+
def permit!
|
148
|
+
each_pair do |key, value|
|
149
|
+
convert_hashes_to_parameters(key, value)
|
150
|
+
self[key].permit! if self[key].respond_to? :permit!
|
151
|
+
end
|
152
|
+
|
153
|
+
@permitted = true
|
154
|
+
self
|
155
|
+
end
|
156
|
+
|
157
|
+
# Ensures that a parameter is present. If it's present, returns
|
158
|
+
# the parameter at the given +key+, otherwise raises an
|
159
|
+
# <tt>ActionController::ParameterMissing</tt> error.
|
160
|
+
#
|
161
|
+
# ActionController::Parameters.new(person: { name: 'Francesco' }).require(:person)
|
162
|
+
# # => {"name"=>"Francesco"}
|
163
|
+
#
|
164
|
+
# ActionController::Parameters.new(person: nil).require(:person)
|
165
|
+
# # => ActionController::ParameterMissing: param not found: person
|
166
|
+
#
|
167
|
+
# ActionController::Parameters.new(person: {}).require(:person)
|
168
|
+
# # => ActionController::ParameterMissing: param not found: person
|
169
|
+
def require(key)
|
170
|
+
self[key].presence || raise(ParameterMissing.new(key))
|
171
|
+
end
|
172
|
+
|
173
|
+
# Alias of #require.
|
174
|
+
alias :required :require
|
175
|
+
|
176
|
+
# Returns a new <tt>ActionController::Parameters</tt> instance that
|
177
|
+
# includes only the given +filters+ and sets the +permitted+ attribute
|
178
|
+
# for the object to +true+. This is useful for limiting which attributes
|
179
|
+
# should be allowed for mass updating.
|
180
|
+
#
|
181
|
+
# params = ActionController::Parameters.new(user: { name: 'Francesco', age: 22, role: 'admin' })
|
182
|
+
# permitted = params.require(:user).permit(:name, :age)
|
183
|
+
# permitted.permitted? # => true
|
184
|
+
# permitted.has_key?(:name) # => true
|
185
|
+
# permitted.has_key?(:age) # => true
|
186
|
+
# permitted.has_key?(:role) # => false
|
187
|
+
#
|
188
|
+
# Only permitted scalars pass the filter. For example, given
|
189
|
+
#
|
190
|
+
# params.permit(:name)
|
191
|
+
#
|
192
|
+
# +:name+ passes it is a key of +params+ whose associated value is of type
|
193
|
+
# +String+, +Symbol+, +NilClass+, +Numeric+, +TrueClass+, +FalseClass+,
|
194
|
+
# +Date+, +Time+, +DateTime+, +StringIO+, +IO+,
|
195
|
+
# +ActionDispatch::Http::UploadedFile+ or +Rack::Test::UploadedFile+.
|
196
|
+
# Otherwise, the key +:name+ is filtered out.
|
197
|
+
#
|
198
|
+
# You may declare that the parameter should be an array of permitted scalars
|
199
|
+
# by mapping it to an empty array:
|
200
|
+
#
|
201
|
+
# params.permit(tags: [])
|
202
|
+
#
|
203
|
+
# You can also use +permit+ on nested parameters, like:
|
204
|
+
#
|
205
|
+
# params = ActionController::Parameters.new({
|
206
|
+
# person: {
|
207
|
+
# name: 'Francesco',
|
208
|
+
# age: 22,
|
209
|
+
# pets: [{
|
210
|
+
# name: 'Purplish',
|
211
|
+
# category: 'dogs'
|
212
|
+
# }]
|
213
|
+
# }
|
214
|
+
# })
|
215
|
+
#
|
216
|
+
# permitted = params.permit(person: [ :name, { pets: :name } ])
|
217
|
+
# permitted.permitted? # => true
|
218
|
+
# permitted[:person][:name] # => "Francesco"
|
219
|
+
# permitted[:person][:age] # => nil
|
220
|
+
# permitted[:person][:pets][0][:name] # => "Purplish"
|
221
|
+
# permitted[:person][:pets][0][:category] # => nil
|
222
|
+
#
|
223
|
+
# Note that if you use +permit+ in a key that points to a hash,
|
224
|
+
# it won't allow all the hash. You also need to specify which
|
225
|
+
# attributes inside the hash should be whitelisted.
|
226
|
+
#
|
227
|
+
# params = ActionController::Parameters.new({
|
228
|
+
# person: {
|
229
|
+
# contact: {
|
230
|
+
# email: 'none@test.com'
|
231
|
+
# phone: '555-1234'
|
232
|
+
# }
|
233
|
+
# }
|
234
|
+
# })
|
235
|
+
#
|
236
|
+
# params.require(:person).permit(:contact)
|
237
|
+
# # => {}
|
238
|
+
#
|
239
|
+
# params.require(:person).permit(contact: :phone)
|
240
|
+
# # => {"contact"=>{"phone"=>"555-1234"}}
|
241
|
+
#
|
242
|
+
# params.require(:person).permit(contact: [ :email, :phone ])
|
243
|
+
# # => {"contact"=>{"email"=>"none@test.com", "phone"=>"555-1234"}}
|
244
|
+
def permit(*filters)
|
245
|
+
params = self.class.new
|
246
|
+
|
247
|
+
filters.flatten.each do |filter|
|
248
|
+
case filter
|
249
|
+
when Symbol, String
|
250
|
+
permitted_scalar_filter(params, filter)
|
251
|
+
when Hash then
|
252
|
+
hash_filter(params, filter)
|
253
|
+
end
|
254
|
+
end
|
255
|
+
|
256
|
+
unpermitted_parameters!(params) if self.class.action_on_unpermitted_parameters
|
257
|
+
|
258
|
+
params.permit!
|
259
|
+
end
|
260
|
+
|
261
|
+
# Returns a parameter for the given +key+. If not found,
|
262
|
+
# returns +nil+.
|
263
|
+
#
|
264
|
+
# params = ActionController::Parameters.new(person: { name: 'Francesco' })
|
265
|
+
# params[:person] # => {"name"=>"Francesco"}
|
266
|
+
# params[:none] # => nil
|
267
|
+
def [](key)
|
268
|
+
convert_hashes_to_parameters(key, super)
|
269
|
+
end
|
270
|
+
|
271
|
+
# Returns a parameter for the given +key+. If the +key+
|
272
|
+
# can't be found, there are several options: With no other arguments,
|
273
|
+
# it will raise an <tt>ActionController::ParameterMissing</tt> error;
|
274
|
+
# if more arguments are given, then that will be returned; if a block
|
275
|
+
# is given, then that will be run and its result returned.
|
276
|
+
#
|
277
|
+
# params = ActionController::Parameters.new(person: { name: 'Francesco' })
|
278
|
+
# params.fetch(:person) # => {"name"=>"Francesco"}
|
279
|
+
# params.fetch(:none) # => ActionController::ParameterMissing: param not found: none
|
280
|
+
# params.fetch(:none, 'Francesco') # => "Francesco"
|
281
|
+
# params.fetch(:none) { 'Francesco' } # => "Francesco"
|
282
|
+
def fetch(key, *args)
|
283
|
+
convert_hashes_to_parameters(key, super)
|
284
|
+
rescue KeyError
|
285
|
+
raise ActionController::ParameterMissing.new(key)
|
286
|
+
end
|
287
|
+
|
288
|
+
# Returns a new <tt>ActionController::Parameters</tt> instance that
|
289
|
+
# includes only the given +keys+. If the given +keys+
|
290
|
+
# don't exist, returns an empty hash.
|
291
|
+
#
|
292
|
+
# params = ActionController::Parameters.new(a: 1, b: 2, c: 3)
|
293
|
+
# params.slice(:a, :b) # => {"a"=>1, "b"=>2}
|
294
|
+
# params.slice(:d) # => {}
|
295
|
+
def slice(*keys)
|
296
|
+
self.class.new(super).tap do |new_instance|
|
297
|
+
new_instance.instance_variable_set :@permitted, @permitted
|
298
|
+
end
|
299
|
+
end
|
300
|
+
|
301
|
+
# Returns an exact copy of the <tt>ActionController::Parameters</tt>
|
302
|
+
# instance. +permitted+ state is kept on the duped object.
|
303
|
+
#
|
304
|
+
# params = ActionController::Parameters.new(a: 1)
|
305
|
+
# params.permit!
|
306
|
+
# params.permitted? # => true
|
307
|
+
# copy_params = params.dup # => {"a"=>1}
|
308
|
+
# copy_params.permitted? # => true
|
309
|
+
def dup
|
310
|
+
super.tap do |duplicate|
|
311
|
+
duplicate.instance_variable_set :@permitted, @permitted
|
312
|
+
end
|
313
|
+
end
|
314
|
+
|
315
|
+
private
|
316
|
+
def convert_hashes_to_parameters(key, value)
|
317
|
+
if value.is_a?(Parameters) || !value.is_a?(Hash)
|
318
|
+
value
|
319
|
+
else
|
320
|
+
# Convert to Parameters on first access
|
321
|
+
self[key] = self.class.new(value)
|
322
|
+
end
|
323
|
+
end
|
324
|
+
|
325
|
+
def each_element(object)
|
326
|
+
if object.is_a?(Array)
|
327
|
+
object.map { |el| yield el }.compact
|
328
|
+
elsif object.is_a?(Hash) && object.keys.all? { |k| k =~ /\A-?\d+\z/ }
|
329
|
+
hash = object.class.new
|
330
|
+
object.each { |k,v| hash[k] = yield v }
|
331
|
+
hash
|
332
|
+
else
|
333
|
+
yield object
|
334
|
+
end
|
335
|
+
end
|
336
|
+
|
337
|
+
def unpermitted_parameters!(params)
|
338
|
+
unpermitted_keys = unpermitted_keys(params)
|
339
|
+
if unpermitted_keys.any?
|
340
|
+
case self.class.action_on_unpermitted_parameters
|
341
|
+
when :log
|
342
|
+
ActionController::Base.logger.debug "Unpermitted parameters: #{unpermitted_keys.join(", ")}"
|
343
|
+
when :raise
|
344
|
+
raise ActionController::UnpermittedParameters.new(unpermitted_keys)
|
345
|
+
end
|
346
|
+
end
|
347
|
+
end
|
348
|
+
|
349
|
+
def unpermitted_keys(params)
|
350
|
+
self.keys - params.keys - NEVER_UNPERMITTED_PARAMS
|
351
|
+
end
|
352
|
+
|
353
|
+
#
|
354
|
+
# --- Filtering ----------------------------------------------------------
|
355
|
+
#
|
356
|
+
|
357
|
+
# This is a white list of permitted scalar types that includes the ones
|
358
|
+
# supported in XML and JSON requests.
|
359
|
+
#
|
360
|
+
# This list is in particular used to filter ordinary requests, String goes
|
361
|
+
# as first element to quickly short-circuit the common case.
|
362
|
+
#
|
363
|
+
# If you modify this collection please update the API of +permit+ above.
|
364
|
+
PERMITTED_SCALAR_TYPES = [
|
365
|
+
String,
|
366
|
+
Symbol,
|
367
|
+
NilClass,
|
368
|
+
Numeric,
|
369
|
+
TrueClass,
|
370
|
+
FalseClass,
|
371
|
+
Date,
|
372
|
+
Time,
|
373
|
+
# DateTimes are Dates, we document the type but avoid the redundant check.
|
374
|
+
StringIO,
|
375
|
+
IO,
|
376
|
+
ActionDispatch::Http::UploadedFile,
|
377
|
+
Rack::Test::UploadedFile,
|
378
|
+
]
|
379
|
+
|
380
|
+
def permitted_scalar?(value)
|
381
|
+
PERMITTED_SCALAR_TYPES.any? {|type| value.is_a?(type)}
|
382
|
+
end
|
383
|
+
|
384
|
+
def permitted_scalar_filter(params, key)
|
385
|
+
if has_key?(key) && permitted_scalar?(self[key])
|
386
|
+
params[key] = self[key]
|
387
|
+
end
|
388
|
+
|
389
|
+
keys.grep(/\A#{Regexp.escape(key)}\(\d+[if]?\)\z/) do |k|
|
390
|
+
if permitted_scalar?(self[k])
|
391
|
+
params[k] = self[k]
|
392
|
+
end
|
393
|
+
end
|
394
|
+
end
|
395
|
+
|
396
|
+
def array_of_permitted_scalars?(value)
|
397
|
+
if value.is_a?(Array)
|
398
|
+
value.all? {|element| permitted_scalar?(element)}
|
399
|
+
end
|
400
|
+
end
|
401
|
+
|
402
|
+
def array_of_permitted_scalars_filter(params, key)
|
403
|
+
if has_key?(key) && array_of_permitted_scalars?(self[key])
|
404
|
+
params[key] = self[key]
|
405
|
+
end
|
406
|
+
end
|
407
|
+
|
408
|
+
EMPTY_ARRAY = []
|
409
|
+
def hash_filter(params, filter)
|
410
|
+
filter = filter.with_indifferent_access
|
411
|
+
|
412
|
+
# Slicing filters out non-declared keys.
|
413
|
+
slice(*filter.keys).each do |key, value|
|
414
|
+
return unless value
|
415
|
+
|
416
|
+
if filter[key] == EMPTY_ARRAY
|
417
|
+
# Declaration { comment_ids: [] }.
|
418
|
+
array_of_permitted_scalars_filter(params, key)
|
419
|
+
else
|
420
|
+
# Declaration { user: :name } or { user: [:name, :age, { adress: ... }] }.
|
421
|
+
params[key] = each_element(value) do |element|
|
422
|
+
if element.is_a?(Hash)
|
423
|
+
element = self.class.new(element) unless element.respond_to?(:permit)
|
424
|
+
element.permit(*Array.wrap(filter[key]))
|
425
|
+
end
|
426
|
+
end
|
427
|
+
end
|
428
|
+
end
|
429
|
+
end
|
430
|
+
end
|
431
|
+
|
432
|
+
# == Strong \Parameters
|
433
|
+
#
|
434
|
+
# It provides an interface for protecting attributes from end-user
|
435
|
+
# assignment. This makes Action Controller parameters forbidden
|
436
|
+
# to be used in Active Model mass assignment until they have been
|
437
|
+
# whitelisted.
|
438
|
+
#
|
439
|
+
# In addition, parameters can be marked as required and flow through a
|
440
|
+
# predefined raise/rescue flow to end up as a 400 Bad Request with no
|
441
|
+
# effort.
|
442
|
+
#
|
443
|
+
# class PeopleController < ActionController::Base
|
444
|
+
# # Using "Person.create(params[:person])" would raise an
|
445
|
+
# # ActiveModel::ForbiddenAttributes exception because it'd
|
446
|
+
# # be using mass assignment without an explicit permit step.
|
447
|
+
# # This is the recommended form:
|
448
|
+
# def create
|
449
|
+
# Person.create(person_params)
|
450
|
+
# end
|
451
|
+
#
|
452
|
+
# # This will pass with flying colors as long as there's a person key in the
|
453
|
+
# # parameters, otherwise it'll raise an ActionController::MissingParameter
|
454
|
+
# # exception, which will get caught by ActionController::Base and turned
|
455
|
+
# # into a 400 Bad Request reply.
|
456
|
+
# def update
|
457
|
+
# redirect_to current_account.people.find(params[:id]).tap { |person|
|
458
|
+
# person.update!(person_params)
|
459
|
+
# }
|
460
|
+
# end
|
461
|
+
#
|
462
|
+
# private
|
463
|
+
# # Using a private method to encapsulate the permissible parameters is
|
464
|
+
# # just a good pattern since you'll be able to reuse the same permit
|
465
|
+
# # list between create and update. Also, you can specialize this method
|
466
|
+
# # with per-user checking of permissible attributes.
|
467
|
+
# def person_params
|
468
|
+
# params.require(:person).permit(:name, :age)
|
469
|
+
# end
|
470
|
+
# end
|
471
|
+
#
|
472
|
+
# In order to use <tt>accepts_nested_attribute_for</tt> with Strong \Parameters, you
|
473
|
+
# will need to specify which nested attributes should be whitelisted.
|
474
|
+
#
|
475
|
+
# class Person
|
476
|
+
# has_many :pets
|
477
|
+
# accepts_nested_attributes_for :pets
|
478
|
+
# end
|
479
|
+
#
|
480
|
+
# class PeopleController < ActionController::Base
|
481
|
+
# def create
|
482
|
+
# Person.create(person_params)
|
483
|
+
# end
|
484
|
+
#
|
485
|
+
# ...
|
486
|
+
#
|
487
|
+
# private
|
488
|
+
#
|
489
|
+
# def person_params
|
490
|
+
# # It's mandatory to specify the nested attributes that should be whitelisted.
|
491
|
+
# # If you use `permit` with just the key that points to the nested attributes hash,
|
492
|
+
# # it will return an empty hash.
|
493
|
+
# params.require(:person).permit(:name, :age, pets_attributes: [ :name, :category ])
|
494
|
+
# end
|
495
|
+
# end
|
496
|
+
#
|
497
|
+
# See ActionController::Parameters.require and ActionController::Parameters.permit
|
498
|
+
# for more information.
|
499
|
+
module StrongParameters
|
500
|
+
extend ActiveSupport::Concern
|
501
|
+
include ActiveSupport::Rescuable
|
502
|
+
|
503
|
+
# Returns a new ActionController::Parameters object that
|
504
|
+
# has been instantiated with the <tt>request.parameters</tt>.
|
505
|
+
def params
|
506
|
+
@_params ||= Parameters.new(request.parameters)
|
507
|
+
end
|
508
|
+
|
509
|
+
# Assigns the given +value+ to the +params+ hash. If +value+
|
510
|
+
# is a Hash, this will create an ActionController::Parameters
|
511
|
+
# object that has been instantiated with the given +value+ hash.
|
512
|
+
def params=(value)
|
513
|
+
@_params = value.is_a?(Hash) ? Parameters.new(value) : value
|
514
|
+
end
|
515
|
+
end
|
516
|
+
end
|