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
@@ -4,13 +4,14 @@ module ActionView
|
|
4
4
|
# Provides a set of methods for making it easier to debug Rails objects.
|
5
5
|
module Helpers
|
6
6
|
module DebugHelper
|
7
|
+
|
8
|
+
include TagHelper
|
9
|
+
|
7
10
|
# Returns a YAML representation of +object+ wrapped with <pre> and </pre>.
|
8
11
|
# If the object cannot be converted to YAML using +to_yaml+, +inspect+ will be called instead.
|
9
12
|
# Useful for inspecting an object at the time of rendering.
|
10
13
|
#
|
11
|
-
#
|
12
|
-
#
|
13
|
-
# @user = User.new({ :username => 'testing', :password => 'xyz', :age => 42}) %>
|
14
|
+
# @user = User.new({ username: 'testing', password: 'xyz', age: 42}) %>
|
14
15
|
# debug(@user)
|
15
16
|
# # =>
|
16
17
|
# <pre class='debug_dump'>--- !ruby/object:User
|
@@ -25,15 +26,13 @@ module ActionView
|
|
25
26
|
#
|
26
27
|
# new_record: true
|
27
28
|
# </pre>
|
28
|
-
|
29
29
|
def debug(object)
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
end
|
30
|
+
Marshal::dump(object)
|
31
|
+
object = ERB::Util.html_escape(object.to_yaml).gsub(" ", " ").html_safe
|
32
|
+
content_tag(:pre, object, :class => "debug_dump")
|
33
|
+
rescue Exception # errors from Marshal or YAML
|
34
|
+
# Object couldn't be dumped, perhaps because of singleton methods -- this is the fallback
|
35
|
+
content_tag(:code, object.inspect, :class => "debug_dump")
|
37
36
|
end
|
38
37
|
end
|
39
38
|
end
|
@@ -3,12 +3,11 @@ require 'action_view/helpers/date_helper'
|
|
3
3
|
require 'action_view/helpers/tag_helper'
|
4
4
|
require 'action_view/helpers/form_tag_helper'
|
5
5
|
require 'action_view/helpers/active_model_helper'
|
6
|
-
require '
|
6
|
+
require 'action_view/helpers/tags'
|
7
|
+
require 'action_view/model_naming'
|
8
|
+
require 'active_support/core_ext/class/attribute_accessors'
|
7
9
|
require 'active_support/core_ext/hash/slice'
|
8
|
-
require 'active_support/core_ext/module/method_names'
|
9
|
-
require 'active_support/core_ext/object/blank'
|
10
10
|
require 'active_support/core_ext/string/output_safety'
|
11
|
-
require 'active_support/core_ext/array/extract_options'
|
12
11
|
require 'active_support/core_ext/string/inflections'
|
13
12
|
|
14
13
|
module ActionView
|
@@ -17,17 +16,28 @@ module ActionView
|
|
17
16
|
# Form helpers are designed to make working with resources much easier
|
18
17
|
# compared to using vanilla HTML.
|
19
18
|
#
|
20
|
-
#
|
21
|
-
#
|
22
|
-
#
|
23
|
-
#
|
19
|
+
# Typically, a form designed to create or update a resource reflects the
|
20
|
+
# identity of the resource in several ways: (i) the url that the form is
|
21
|
+
# sent to (the form element's +action+ attribute) should result in a request
|
22
|
+
# being routed to the appropriate controller action (with the appropriate <tt>:id</tt>
|
23
|
+
# parameter in the case of an existing resource), (ii) input fields should
|
24
|
+
# be named in such a way that in the controller their values appear in the
|
25
|
+
# appropriate places within the +params+ hash, and (iii) for an existing record,
|
26
|
+
# when the form is initially displayed, input fields corresponding to attributes
|
27
|
+
# of the resource should show the current values of those attributes.
|
24
28
|
#
|
25
|
-
#
|
26
|
-
#
|
29
|
+
# In Rails, this is usually achieved by creating the form using +form_for+ and
|
30
|
+
# a number of related helper methods. +form_for+ generates an appropriate <tt>form</tt>
|
31
|
+
# tag and yields a form builder object that knows the model the form is about.
|
32
|
+
# Input fields are created by calling methods defined on the form builder, which
|
33
|
+
# means they are able to generate the appropriate names and default values
|
34
|
+
# corresponding to the model attributes, as well as convenient IDs, etc.
|
35
|
+
# Conventions in the generated field names allow controllers to receive form data
|
36
|
+
# nicely structured in +params+ with no effort on your side.
|
27
37
|
#
|
28
38
|
# For example, to create a new person you typically set up a new instance of
|
29
39
|
# +Person+ in the <tt>PeopleController#new</tt> action, <tt>@person</tt>, and
|
30
|
-
# pass
|
40
|
+
# in the view template pass that object to +form_for+:
|
31
41
|
#
|
32
42
|
# <%= form_for @person do |f| %>
|
33
43
|
# <%= f.label :first_name %>:
|
@@ -46,10 +56,10 @@ module ActionView
|
|
46
56
|
# <input name="authenticity_token" type="hidden" value="NrOp5bsjoLRuK8IW5+dQEYjKGUJDe7TQoZVvq95Wteg=" />
|
47
57
|
# </div>
|
48
58
|
# <label for="person_first_name">First name</label>:
|
49
|
-
# <input id="person_first_name" name="person[first_name]"
|
59
|
+
# <input id="person_first_name" name="person[first_name]" type="text" /><br />
|
50
60
|
#
|
51
61
|
# <label for="person_last_name">Last name</label>:
|
52
|
-
# <input id="person_last_name" name="person[last_name]"
|
62
|
+
# <input id="person_last_name" name="person[last_name]" type="text" /><br />
|
53
63
|
#
|
54
64
|
# <input name="commit" type="submit" value="Create Person" />
|
55
65
|
# </form>
|
@@ -73,14 +83,14 @@ module ActionView
|
|
73
83
|
#
|
74
84
|
# <form action="/people/256" class="edit_person" id="edit_person_256" method="post">
|
75
85
|
# <div style="margin:0;padding:0;display:inline">
|
76
|
-
# <input name="_method" type="hidden" value="
|
86
|
+
# <input name="_method" type="hidden" value="patch" />
|
77
87
|
# <input name="authenticity_token" type="hidden" value="NrOp5bsjoLRuK8IW5+dQEYjKGUJDe7TQoZVvq95Wteg=" />
|
78
88
|
# </div>
|
79
89
|
# <label for="person_first_name">First name</label>:
|
80
|
-
# <input id="person_first_name" name="person[first_name]"
|
90
|
+
# <input id="person_first_name" name="person[first_name]" type="text" value="John" /><br />
|
81
91
|
#
|
82
92
|
# <label for="person_last_name">Last name</label>:
|
83
|
-
# <input id="person_last_name" name="person[last_name]"
|
93
|
+
# <input id="person_last_name" name="person[last_name]" type="text" value="Smith" /><br />
|
84
94
|
#
|
85
95
|
# <input name="commit" type="submit" value="Update Person" />
|
86
96
|
# </form>
|
@@ -90,9 +100,9 @@ module ActionView
|
|
90
100
|
# and generate HTML accordingly.
|
91
101
|
#
|
92
102
|
# The controller would receive the form data again in <tt>params[:person]</tt>, ready to be
|
93
|
-
# passed to <tt>Person#
|
103
|
+
# passed to <tt>Person#update</tt>:
|
94
104
|
#
|
95
|
-
# if @person.
|
105
|
+
# if @person.update(params[:person])
|
96
106
|
# # success
|
97
107
|
# else
|
98
108
|
# # error handling
|
@@ -104,35 +114,16 @@ module ActionView
|
|
104
114
|
|
105
115
|
include FormTagHelper
|
106
116
|
include UrlHelper
|
117
|
+
include ModelNaming
|
107
118
|
|
108
|
-
#
|
109
|
-
|
110
|
-
object.respond_to?(:to_model) ? object.to_model : object
|
111
|
-
end
|
112
|
-
|
113
|
-
# Creates a form and a scope around a specific model object that is used
|
114
|
-
# as a base for questioning about values for the fields.
|
115
|
-
#
|
116
|
-
# Rails provides succinct resource-oriented form generation with +form_for+
|
117
|
-
# like this:
|
118
|
-
#
|
119
|
-
# <%= form_for @offer do |f| %>
|
120
|
-
# <%= f.label :version, 'Version' %>:
|
121
|
-
# <%= f.text_field :version %><br />
|
122
|
-
# <%= f.label :author, 'Author' %>:
|
123
|
-
# <%= f.text_field :author %><br />
|
124
|
-
# <%= f.submit %>
|
125
|
-
# <% end %>
|
126
|
-
#
|
127
|
-
# There, +form_for+ is able to generate the rest of RESTful form
|
128
|
-
# parameters based on introspection on the record, but to understand what
|
129
|
-
# it does we need to dig first into the alternative generic usage it is
|
130
|
-
# based upon.
|
131
|
-
#
|
132
|
-
# === Generic form_for
|
119
|
+
# Creates a form that allows the user to create or update the attributes
|
120
|
+
# of a specific model object.
|
133
121
|
#
|
134
|
-
# The
|
135
|
-
# model
|
122
|
+
# The method can be used in several slightly different ways, depending on
|
123
|
+
# how much you wish to rely on Rails to infer automatically from the model
|
124
|
+
# how the form should be constructed. For a generic model object, a form
|
125
|
+
# can be created by passing +form_for+ a string or symbol representing
|
126
|
+
# the object we are concerned with:
|
136
127
|
#
|
137
128
|
# <%= form_for :person do |f| %>
|
138
129
|
# First name: <%= f.text_field :first_name %><br />
|
@@ -142,24 +133,39 @@ module ActionView
|
|
142
133
|
# <%= f.submit %>
|
143
134
|
# <% end %>
|
144
135
|
#
|
145
|
-
#
|
146
|
-
#
|
147
|
-
#
|
148
|
-
#
|
149
|
-
# model. Thus, the idea is that
|
136
|
+
# The variable +f+ yielded to the block is a FormBuilder object that
|
137
|
+
# incorporates the knowledge about the model object represented by
|
138
|
+
# <tt>:person</tt> passed to +form_for+. Methods defined on the FormBuilder
|
139
|
+
# are used to generate fields bound to this model. Thus, for example,
|
150
140
|
#
|
151
141
|
# <%= f.text_field :first_name %>
|
152
142
|
#
|
153
|
-
#
|
143
|
+
# will get expanded to
|
154
144
|
#
|
155
145
|
# <%= text_field :person, :first_name %>
|
146
|
+
# which results in an html <tt><input></tt> tag whose +name+ attribute is
|
147
|
+
# <tt>person[first_name]</tt>. This means that when the form is submitted,
|
148
|
+
# the value entered by the user will be available in the controller as
|
149
|
+
# <tt>params[:person][:first_name]</tt>.
|
150
|
+
#
|
151
|
+
# For fields generated in this way using the FormBuilder,
|
152
|
+
# if <tt>:person</tt> also happens to be the name of an instance variable
|
153
|
+
# <tt>@person</tt>, the default value of the field shown when the form is
|
154
|
+
# initially displayed (e.g. in the situation where you are editing an
|
155
|
+
# existing record) will be the value of the corresponding attribute of
|
156
|
+
# <tt>@person</tt>.
|
156
157
|
#
|
157
158
|
# The rightmost argument to +form_for+ is an
|
158
|
-
# optional hash of options
|
159
|
-
#
|
160
|
-
# * <tt>:url</tt> - The URL the form is submitted to.
|
161
|
-
#
|
162
|
-
#
|
159
|
+
# optional hash of options -
|
160
|
+
#
|
161
|
+
# * <tt>:url</tt> - The URL the form is to be submitted to. This may be
|
162
|
+
# represented in the same way as values passed to +url_for+ or +link_to+.
|
163
|
+
# So for example you may use a named route directly. When the model is
|
164
|
+
# represented by a string or symbol, as in the example above, if the
|
165
|
+
# <tt>:url</tt> option is not specified, by default the form will be
|
166
|
+
# sent back to the current url (We will describe below an alternative
|
167
|
+
# resource-oriented usage of +form_for+ in which the URL does not need
|
168
|
+
# to be specified explicitly).
|
163
169
|
# * <tt>:namespace</tt> - A namespace for your form to ensure uniqueness of
|
164
170
|
# id attributes on form elements. The namespace attribute will be prefixed
|
165
171
|
# with underscore on the generated HTML id.
|
@@ -169,11 +175,11 @@ module ActionView
|
|
169
175
|
# possible to use both the stand-alone FormHelper methods and methods
|
170
176
|
# from FormTagHelper. For example:
|
171
177
|
#
|
172
|
-
# <%= form_for
|
178
|
+
# <%= form_for :person do |f| %>
|
173
179
|
# First name: <%= f.text_field :first_name %>
|
174
180
|
# Last name : <%= f.text_field :last_name %>
|
175
181
|
# Biography : <%= text_area :person, :biography %>
|
176
|
-
# Admin? : <%= check_box_tag "person[admin]", @person.company.admin? %>
|
182
|
+
# Admin? : <%= check_box_tag "person[admin]", "1", @person.company.admin? %>
|
177
183
|
# <%= f.submit %>
|
178
184
|
# <% end %>
|
179
185
|
#
|
@@ -181,53 +187,85 @@ module ActionView
|
|
181
187
|
# are designed to work with an object as base, like
|
182
188
|
# FormOptionHelper#collection_select and DateHelper#datetime_select.
|
183
189
|
#
|
184
|
-
# ===
|
185
|
-
#
|
186
|
-
# As we said above, in addition to manually configuring the +form_for+
|
187
|
-
# call, you can rely on automated resource identification, which will use
|
188
|
-
# the conventions and named routes of that approach. This is the
|
189
|
-
# preferred way to use +form_for+ nowadays.
|
190
|
+
# === #form_for with a model object
|
190
191
|
#
|
191
|
-
#
|
192
|
+
# In the examples above, the object to be created or edited was
|
193
|
+
# represented by a symbol passed to +form_for+, and we noted that
|
194
|
+
# a string can also be used equivalently. It is also possible, however,
|
195
|
+
# to pass a model object itself to +form_for+. For example, if <tt>@post</tt>
|
196
|
+
# is an existing record you wish to edit, you can create the form using
|
192
197
|
#
|
193
198
|
# <%= form_for @post do |f| %>
|
194
199
|
# ...
|
195
200
|
# <% end %>
|
196
201
|
#
|
197
|
-
#
|
202
|
+
# This behaves in almost the same way as outlined previously, with a
|
203
|
+
# couple of small exceptions. First, the prefix used to name the input
|
204
|
+
# elements within the form (hence the key that denotes them in the +params+
|
205
|
+
# hash) is actually derived from the object's _class_, e.g. <tt>params[:post]</tt>
|
206
|
+
# if the object's class is +Post+. However, this can be overwritten using
|
207
|
+
# the <tt>:as</tt> option, e.g. -
|
198
208
|
#
|
199
|
-
# <%= form_for
|
209
|
+
# <%= form_for(@person, as: :client) do |f| %>
|
200
210
|
# ...
|
201
211
|
# <% end %>
|
202
212
|
#
|
203
|
-
#
|
213
|
+
# would result in <tt>params[:client]</tt>.
|
204
214
|
#
|
205
|
-
#
|
215
|
+
# Secondly, the field values shown when the form is initially displayed
|
216
|
+
# are taken from the attributes of the object passed to +form_for+,
|
217
|
+
# regardless of whether the object is an instance
|
218
|
+
# variable. So, for example, if we had a _local_ variable +post+
|
219
|
+
# representing an existing record,
|
220
|
+
#
|
221
|
+
# <%= form_for post do |f| %>
|
206
222
|
# ...
|
207
223
|
# <% end %>
|
208
224
|
#
|
209
|
-
#
|
225
|
+
# would produce a form with fields whose initial state reflect the current
|
226
|
+
# values of the attributes of +post+.
|
227
|
+
#
|
228
|
+
# === Resource-oriented style
|
229
|
+
#
|
230
|
+
# In the examples just shown, although not indicated explicitly, we still
|
231
|
+
# need to use the <tt>:url</tt> option in order to specify where the
|
232
|
+
# form is going to be sent. However, further simplification is possible
|
233
|
+
# if the record passed to +form_for+ is a _resource_, i.e. it corresponds
|
234
|
+
# to a set of RESTful routes, e.g. defined using the +resources+ method
|
235
|
+
# in <tt>config/routes.rb</tt>. In this case Rails will simply infer the
|
236
|
+
# appropriate URL from the record itself. For example,
|
210
237
|
#
|
211
|
-
# <%= form_for @post
|
238
|
+
# <%= form_for @post do |f| %>
|
212
239
|
# ...
|
213
240
|
# <% end %>
|
214
241
|
#
|
215
|
-
#
|
242
|
+
# is then equivalent to something like:
|
216
243
|
#
|
217
|
-
# <%= form_for
|
244
|
+
# <%= form_for @post, as: :post, url: post_path(@post), method: :patch, html: { class: "edit_post", id: "edit_post_45" } do |f| %>
|
218
245
|
# ...
|
219
246
|
# <% end %>
|
220
247
|
#
|
221
|
-
#
|
248
|
+
# And for a new record
|
249
|
+
#
|
250
|
+
# <%= form_for(Post.new) do |f| %>
|
251
|
+
# ...
|
252
|
+
# <% end %>
|
253
|
+
#
|
254
|
+
# is equivalent to something like:
|
255
|
+
#
|
256
|
+
# <%= form_for @post, as: :post, url: posts_path, html: { class: "new_post", id: "new_post" } do |f| %>
|
257
|
+
# ...
|
258
|
+
# <% end %>
|
259
|
+
#
|
260
|
+
# However you can still overwrite individual conventions, such as:
|
222
261
|
#
|
223
|
-
# <%= form_for(@post, :
|
262
|
+
# <%= form_for(@post, url: super_posts_path) do |f| %>
|
224
263
|
# ...
|
225
264
|
# <% end %>
|
226
265
|
#
|
227
|
-
#
|
228
|
-
# parameter, like a Person that acts as a Client:
|
266
|
+
# You can also set the answer format, like this:
|
229
267
|
#
|
230
|
-
# <%= form_for(@
|
268
|
+
# <%= form_for(@post, format: :json) do |f| %>
|
231
269
|
# ...
|
232
270
|
# <% end %>
|
233
271
|
#
|
@@ -251,17 +289,17 @@ module ActionView
|
|
251
289
|
#
|
252
290
|
# You can force the form to use the full array of HTTP verbs by setting
|
253
291
|
#
|
254
|
-
# :
|
292
|
+
# method: (:get|:post|:patch|:put|:delete)
|
255
293
|
#
|
256
|
-
# in the options hash. If the verb is not GET or POST, which are natively
|
257
|
-
# form will be set to POST and a hidden input
|
258
|
-
# to interpret.
|
294
|
+
# in the options hash. If the verb is not GET or POST, which are natively
|
295
|
+
# supported by HTML forms, the form will be set to POST and a hidden input
|
296
|
+
# called _method will carry the intended verb for the server to interpret.
|
259
297
|
#
|
260
298
|
# === Unobtrusive JavaScript
|
261
299
|
#
|
262
300
|
# Specifying:
|
263
301
|
#
|
264
|
-
# :
|
302
|
+
# remote: true
|
265
303
|
#
|
266
304
|
# in the options hash creates a form that will allow the unobtrusive JavaScript drivers to modify its
|
267
305
|
# behavior. The expected default behavior is an XMLHttpRequest in the background instead of the regular
|
@@ -271,7 +309,7 @@ module ActionView
|
|
271
309
|
#
|
272
310
|
# Example:
|
273
311
|
#
|
274
|
-
# <%= form_for(@post, :
|
312
|
+
# <%= form_for(@post, remote: true) do |f| %>
|
275
313
|
# ...
|
276
314
|
# <% end %>
|
277
315
|
#
|
@@ -279,7 +317,25 @@ module ActionView
|
|
279
317
|
#
|
280
318
|
# <form action='http://www.example.com' method='post' data-remote='true'>
|
281
319
|
# <div style='margin:0;padding:0;display:inline'>
|
282
|
-
# <input name='_method' type='hidden' value='
|
320
|
+
# <input name='_method' type='hidden' value='patch' />
|
321
|
+
# </div>
|
322
|
+
# ...
|
323
|
+
# </form>
|
324
|
+
#
|
325
|
+
# === Setting HTML options
|
326
|
+
#
|
327
|
+
# You can set data attributes directly by passing in a data hash, but all other HTML options must be wrapped in
|
328
|
+
# the HTML key. Example:
|
329
|
+
#
|
330
|
+
# <%= form_for(@post, data: { behavior: "autosave" }, html: { name: "go" }) do |f| %>
|
331
|
+
# ...
|
332
|
+
# <% end %>
|
333
|
+
#
|
334
|
+
# The HTML generated for this would be:
|
335
|
+
#
|
336
|
+
# <form action='http://www.example.com' method='post' data-behavior='autosave' name='go'>
|
337
|
+
# <div style='margin:0;padding:0;display:inline'>
|
338
|
+
# <input name='_method' type='hidden' value='patch' />
|
283
339
|
# </div>
|
284
340
|
# ...
|
285
341
|
# </form>
|
@@ -297,7 +353,7 @@ module ActionView
|
|
297
353
|
# Example:
|
298
354
|
#
|
299
355
|
# <%= form_for(@post) do |f| %>
|
300
|
-
#
|
356
|
+
# <%= f.fields_for(:comments, include_id: false) do |cf| %>
|
301
357
|
# ...
|
302
358
|
# <% end %>
|
303
359
|
# <% end %>
|
@@ -309,7 +365,7 @@ module ActionView
|
|
309
365
|
# custom builder. For example, let's say you made a helper to
|
310
366
|
# automatically add labels to form inputs.
|
311
367
|
#
|
312
|
-
# <%= form_for @person, :
|
368
|
+
# <%= form_for @person, url: { action: "create" }, builder: LabellingFormBuilder do |f| %>
|
313
369
|
# <%= f.text_field :first_name %>
|
314
370
|
# <%= f.text_field :last_name %>
|
315
371
|
# <%= f.text_area :biography %>
|
@@ -333,7 +389,7 @@ module ActionView
|
|
333
389
|
#
|
334
390
|
# def labelled_form_for(record_or_name_or_array, *args, &block)
|
335
391
|
# options = args.extract_options!
|
336
|
-
# form_for(record_or_name_or_array, *(args << options.merge(:
|
392
|
+
# form_for(record_or_name_or_array, *(args << options.merge(builder: LabellingFormBuilder)), &block)
|
337
393
|
# end
|
338
394
|
#
|
339
395
|
# If you don't need to attach a form to a model instance, then check out
|
@@ -346,19 +402,18 @@ module ActionView
|
|
346
402
|
#
|
347
403
|
# To set an authenticity token you need to pass an <tt>:authenticity_token</tt> parameter
|
348
404
|
#
|
349
|
-
# <%= form_for @invoice, :
|
405
|
+
# <%= form_for @invoice, url: external_url, authenticity_token: 'external_token' do |f|
|
350
406
|
# ...
|
351
407
|
# <% end %>
|
352
408
|
#
|
353
409
|
# If you don't want to an authenticity token field be rendered at all just pass <tt>false</tt>:
|
354
410
|
#
|
355
|
-
# <%= form_for @invoice, :
|
411
|
+
# <%= form_for @invoice, url: external_url, authenticity_token: false do |f|
|
356
412
|
# ...
|
357
413
|
# <% end %>
|
358
414
|
def form_for(record, options = {}, &block)
|
359
415
|
raise ArgumentError, "Missing block" unless block_given?
|
360
|
-
|
361
|
-
options[:html] ||= {}
|
416
|
+
html_options = options[:html] ||= {}
|
362
417
|
|
363
418
|
case record
|
364
419
|
when String, Symbol
|
@@ -366,33 +421,35 @@ module ActionView
|
|
366
421
|
object = nil
|
367
422
|
else
|
368
423
|
object = record.is_a?(Array) ? record.last : record
|
369
|
-
|
370
|
-
|
424
|
+
raise ArgumentError, "First argument in form cannot contain nil or be empty" unless object
|
425
|
+
object_name = options[:as] || model_name_from_record_or_class(object).param_key
|
426
|
+
apply_form_for_options!(record, object, options)
|
371
427
|
end
|
372
428
|
|
373
|
-
|
374
|
-
|
375
|
-
|
429
|
+
html_options[:data] = options.delete(:data) if options.has_key?(:data)
|
430
|
+
html_options[:remote] = options.delete(:remote) if options.has_key?(:remote)
|
431
|
+
html_options[:method] = options.delete(:method) if options.has_key?(:method)
|
432
|
+
html_options[:authenticity_token] = options.delete(:authenticity_token)
|
376
433
|
|
377
|
-
builder =
|
434
|
+
builder = instantiate_builder(object_name, object, options)
|
378
435
|
output = capture(builder, &block)
|
379
|
-
|
380
|
-
|
436
|
+
html_options[:multipart] = builder.multipart?
|
437
|
+
|
438
|
+
form_tag(options[:url] || {}, html_options) { output }
|
381
439
|
end
|
382
440
|
|
383
|
-
def apply_form_for_options!(
|
384
|
-
object = object_or_array.is_a?(Array) ? object_or_array.last : object_or_array
|
441
|
+
def apply_form_for_options!(record, object, options) #:nodoc:
|
385
442
|
object = convert_to_model(object)
|
386
443
|
|
387
444
|
as = options[:as]
|
388
|
-
action, method = object.respond_to?(:persisted?) && object.persisted? ? [:edit, :
|
445
|
+
action, method = object.respond_to?(:persisted?) && object.persisted? ? [:edit, :patch] : [:new, :post]
|
389
446
|
options[:html].reverse_merge!(
|
390
|
-
:
|
391
|
-
:
|
392
|
-
:
|
447
|
+
class: as ? "#{action}_#{as}" : dom_class(object, action),
|
448
|
+
id: as ? "#{action}_#{as}" : [options[:namespace], dom_id(object, action)].compact.join("_").presence,
|
449
|
+
method: method
|
393
450
|
)
|
394
451
|
|
395
|
-
options[:url] ||= polymorphic_path(
|
452
|
+
options[:url] ||= polymorphic_path(record, format: options.delete(:format))
|
396
453
|
end
|
397
454
|
private :apply_form_for_options!
|
398
455
|
|
@@ -400,32 +457,59 @@ module ActionView
|
|
400
457
|
# doesn't create the form tags themselves. This makes fields_for suitable
|
401
458
|
# for specifying additional model objects in the same form.
|
402
459
|
#
|
403
|
-
#
|
460
|
+
# Although the usage and purpose of +field_for+ is similar to +form_for+'s,
|
461
|
+
# its method signature is slightly different. Like +form_for+, it yields
|
462
|
+
# a FormBuilder object associated with a particular model object to a block,
|
463
|
+
# and within the block allows methods to be called on the builder to
|
464
|
+
# generate fields associated with the model object. Fields may reflect
|
465
|
+
# a model object in two ways - how they are named (hence how submitted
|
466
|
+
# values appear within the +params+ hash in the controller) and what
|
467
|
+
# default values are shown when the form the fields appear in is first
|
468
|
+
# displayed. In order for both of these features to be specified independently,
|
469
|
+
# both an object name (represented by either a symbol or string) and the
|
470
|
+
# object itself can be passed to the method separately -
|
404
471
|
#
|
405
472
|
# <%= form_for @person do |person_form| %>
|
406
473
|
# First name: <%= person_form.text_field :first_name %>
|
407
474
|
# Last name : <%= person_form.text_field :last_name %>
|
408
475
|
#
|
409
|
-
# <%= fields_for @person.permission do |permission_fields| %>
|
476
|
+
# <%= fields_for :permission, @person.permission do |permission_fields| %>
|
410
477
|
# Admin? : <%= permission_fields.check_box :admin %>
|
411
478
|
# <% end %>
|
412
479
|
#
|
413
480
|
# <%= f.submit %>
|
414
481
|
# <% end %>
|
415
482
|
#
|
416
|
-
#
|
417
|
-
#
|
483
|
+
# In this case, the checkbox field will be represented by an HTML +input+
|
484
|
+
# tag with the +name+ attribute <tt>permission[admin]</tt>, and the submitted
|
485
|
+
# value will appear in the controller as <tt>params[:permission][:admin]</tt>.
|
486
|
+
# If <tt>@person.permission</tt> is an existing record with an attribute
|
487
|
+
# +admin+, the initial state of the checkbox when first displayed will
|
488
|
+
# reflect the value of <tt>@person.permission.admin</tt>.
|
489
|
+
#
|
490
|
+
# Often this can be simplified by passing just the name of the model
|
491
|
+
# object to +fields_for+ -
|
418
492
|
#
|
419
|
-
# <%= fields_for :
|
493
|
+
# <%= fields_for :permission do |permission_fields| %>
|
420
494
|
# Admin?: <%= permission_fields.check_box :admin %>
|
421
495
|
# <% end %>
|
422
496
|
#
|
423
|
-
# ...
|
497
|
+
# ...in which case, if <tt>:permission</tt> also happens to be the name of an
|
498
|
+
# instance variable <tt>@permission</tt>, the initial state of the input
|
499
|
+
# field will reflect the value of that variable's attribute <tt>@permission.admin</tt>.
|
424
500
|
#
|
425
|
-
#
|
501
|
+
# Alternatively, you can pass just the model object itself (if the first
|
502
|
+
# argument isn't a string or symbol +fields_for+ will realize that the
|
503
|
+
# name has been omitted) -
|
504
|
+
#
|
505
|
+
# <%= fields_for @person.permission do |permission_fields| %>
|
426
506
|
# Admin?: <%= permission_fields.check_box :admin %>
|
427
507
|
# <% end %>
|
428
508
|
#
|
509
|
+
# and +fields_for+ will derive the required name of the field from the
|
510
|
+
# _class_ of the model object, e.g. if <tt>@person.permission</tt>, is
|
511
|
+
# of class +Permission+, the field will still be named <tt>permission[admin]</tt>.
|
512
|
+
#
|
429
513
|
# Note: This also works for the methods in FormOptionHelper and
|
430
514
|
# DateHelper that are designed to work with an object as base, like
|
431
515
|
# FormOptionHelper#collection_select and DateHelper#datetime_select.
|
@@ -489,7 +573,7 @@ module ActionView
|
|
489
573
|
#
|
490
574
|
# class Person < ActiveRecord::Base
|
491
575
|
# has_one :address
|
492
|
-
# accepts_nested_attributes_for :address, :
|
576
|
+
# accepts_nested_attributes_for :address, allow_destroy: true
|
493
577
|
# end
|
494
578
|
#
|
495
579
|
# Now, when you use a form element with the <tt>_destroy</tt> parameter,
|
@@ -585,7 +669,7 @@ module ActionView
|
|
585
669
|
#
|
586
670
|
# class Person < ActiveRecord::Base
|
587
671
|
# has_many :projects
|
588
|
-
# accepts_nested_attributes_for :projects, :
|
672
|
+
# accepts_nested_attributes_for :projects, allow_destroy: true
|
589
673
|
# end
|
590
674
|
#
|
591
675
|
# This will allow you to specify which models to destroy in the
|
@@ -600,11 +684,27 @@ module ActionView
|
|
600
684
|
# <% end %>
|
601
685
|
# ...
|
602
686
|
# <% end %>
|
687
|
+
#
|
688
|
+
# When a collection is used you might want to know the index of each
|
689
|
+
# object into the array. For this purpose, the <tt>index</tt> method
|
690
|
+
# is available in the FormBuilder object.
|
691
|
+
#
|
692
|
+
# <%= form_for @person do |person_form| %>
|
693
|
+
# ...
|
694
|
+
# <%= person_form.fields_for :projects do |project_fields| %>
|
695
|
+
# Project #<%= project_fields.index %>
|
696
|
+
# ...
|
697
|
+
# <% end %>
|
698
|
+
# ...
|
699
|
+
# <% end %>
|
700
|
+
#
|
701
|
+
# Note that fields_for will automatically generate a hidden field
|
702
|
+
# to store the ID of the record. There are circumstances where this
|
703
|
+
# hidden field is not needed and you can pass <tt>hidden_field_id: false</tt>
|
704
|
+
# to prevent fields_for from rendering it automatically.
|
603
705
|
def fields_for(record_name, record_object = nil, options = {}, &block)
|
604
|
-
builder = instantiate_builder(record_name, record_object, options
|
605
|
-
|
606
|
-
output.concat builder.hidden_field(:id) if output && options[:hidden_field_id] && !builder.emitted_hidden_id?
|
607
|
-
output
|
706
|
+
builder = instantiate_builder(record_name, record_object, options)
|
707
|
+
capture(builder, &block)
|
608
708
|
end
|
609
709
|
|
610
710
|
# Returns a label tag tailored for labelling an input field for a specified attribute (identified by +method+) on an object
|
@@ -618,15 +718,15 @@ module ActionView
|
|
618
718
|
# label(:post, :title)
|
619
719
|
# # => <label for="post_title">Title</label>
|
620
720
|
#
|
621
|
-
#
|
622
|
-
#
|
721
|
+
# You can localize your labels based on model and attribute names.
|
722
|
+
# For example you can define the following in your locale (e.g. en.yml)
|
623
723
|
#
|
624
724
|
# helpers:
|
625
725
|
# label:
|
626
726
|
# post:
|
627
727
|
# body: "Write your entire text here"
|
628
728
|
#
|
629
|
-
#
|
729
|
+
# Which then will result in
|
630
730
|
#
|
631
731
|
# label(:post, :body)
|
632
732
|
# # => <label for="post_body">Write your entire text here</label>
|
@@ -645,27 +745,17 @@ module ActionView
|
|
645
745
|
# label(:post, :title, "A short title")
|
646
746
|
# # => <label for="post_title">A short title</label>
|
647
747
|
#
|
648
|
-
# label(:post, :title, "A short title", :
|
748
|
+
# label(:post, :title, "A short title", class: "title_label")
|
649
749
|
# # => <label for="post_title" class="title_label">A short title</label>
|
650
750
|
#
|
651
|
-
# label(:post, :privacy, "Public Post", :
|
751
|
+
# label(:post, :privacy, "Public Post", value: "public")
|
652
752
|
# # => <label for="post_privacy_public">Public Post</label>
|
653
753
|
#
|
654
754
|
# label(:post, :terms) do
|
655
755
|
# 'Accept <a href="/terms">Terms</a>.'.html_safe
|
656
756
|
# end
|
657
757
|
def label(object_name, method, content_or_options = nil, options = nil, &block)
|
658
|
-
|
659
|
-
|
660
|
-
content_is_options = content_or_options.is_a?(Hash)
|
661
|
-
if content_is_options || block_given?
|
662
|
-
options.merge!(content_or_options) if content_is_options
|
663
|
-
text = nil
|
664
|
-
else
|
665
|
-
text = content_or_options
|
666
|
-
end
|
667
|
-
|
668
|
-
InstanceTag.new(object_name, method, self, options.delete(:object)).to_label_tag(text, options, &block)
|
758
|
+
Tags::Label.new(object_name, method, self, content_or_options, options).render(&block)
|
669
759
|
end
|
670
760
|
|
671
761
|
# Returns an input tag of the "text" type tailored for accessing a specified attribute (identified by +method+) on an object
|
@@ -674,42 +764,40 @@ module ActionView
|
|
674
764
|
# shown.
|
675
765
|
#
|
676
766
|
# ==== Examples
|
677
|
-
# text_field(:post, :title, :
|
767
|
+
# text_field(:post, :title, size: 20)
|
678
768
|
# # => <input type="text" id="post_title" name="post[title]" size="20" value="#{@post.title}" />
|
679
769
|
#
|
680
|
-
# text_field(:post, :title, :
|
770
|
+
# text_field(:post, :title, class: "create_input")
|
681
771
|
# # => <input type="text" id="post_title" name="post[title]" value="#{@post.title}" class="create_input" />
|
682
772
|
#
|
683
|
-
# text_field(:session, :user, :
|
684
|
-
# # => <input type="text" id="session_user" name="session[user]" value="#{@session.user}" onchange
|
773
|
+
# text_field(:session, :user, onchange: "if ($('#session_user').val() === 'admin') { alert('Your login can not be admin!'); }")
|
774
|
+
# # => <input type="text" id="session_user" name="session[user]" value="#{@session.user}" onchange="if ($('#session_user').val() === 'admin') { alert('Your login can not be admin!'); }"/>
|
685
775
|
#
|
686
|
-
# text_field(:snippet, :code, :
|
776
|
+
# text_field(:snippet, :code, size: 20, class: 'code_input')
|
687
777
|
# # => <input type="text" id="snippet_code" name="snippet[code]" size="20" value="#{@snippet.code}" class="code_input" />
|
688
|
-
#
|
689
778
|
def text_field(object_name, method, options = {})
|
690
|
-
|
779
|
+
Tags::TextField.new(object_name, method, self, options).render
|
691
780
|
end
|
692
781
|
|
693
782
|
# Returns an input tag of the "password" type tailored for accessing a specified attribute (identified by +method+) on an object
|
694
783
|
# assigned to the template (identified by +object+). Additional options on the input tag can be passed as a
|
695
784
|
# hash with +options+. These options will be tagged onto the HTML as an HTML element attribute as in the example
|
696
|
-
# shown.
|
785
|
+
# shown. For security reasons this field is blank by default; pass in a value via +options+ if this is not desired.
|
697
786
|
#
|
698
787
|
# ==== Examples
|
699
|
-
# password_field(:login, :pass, :
|
788
|
+
# password_field(:login, :pass, size: 20)
|
700
789
|
# # => <input type="password" id="login_pass" name="login[pass]" size="20" />
|
701
790
|
#
|
702
|
-
# password_field(:account, :secret, :
|
791
|
+
# password_field(:account, :secret, class: "form_input", value: @account.secret)
|
703
792
|
# # => <input type="password" id="account_secret" name="account[secret]" value="#{@account.secret}" class="form_input" />
|
704
793
|
#
|
705
|
-
# password_field(:user, :password, :
|
706
|
-
# # => <input type="password" id="user_password" name="user[password]" onchange
|
794
|
+
# password_field(:user, :password, onchange: "if ($('#user_password').val().length > 30) { alert('Your password needs to be shorter!'); }")
|
795
|
+
# # => <input type="password" id="user_password" name="user[password]" onchange="if ($('#user_password').val().length > 30) { alert('Your password needs to be shorter!'); }"/>
|
707
796
|
#
|
708
|
-
# password_field(:account, :pin, :
|
797
|
+
# password_field(:account, :pin, size: 20, class: 'form_input')
|
709
798
|
# # => <input type="password" id="account_pin" name="account[pin]" size="20" class="form_input" />
|
710
|
-
#
|
711
799
|
def password_field(object_name, method, options = {})
|
712
|
-
|
800
|
+
Tags::PasswordField.new(object_name, method, self, options).render
|
713
801
|
end
|
714
802
|
|
715
803
|
# Returns a hidden input tag tailored for accessing a specified attribute (identified by +method+) on an object
|
@@ -727,7 +815,7 @@ module ActionView
|
|
727
815
|
# hidden_field(:user, :token)
|
728
816
|
# # => <input type="hidden" id="user_token" name="user[token]" value="#{@user.token}" />
|
729
817
|
def hidden_field(object_name, method, options = {})
|
730
|
-
|
818
|
+
Tags::HiddenField.new(object_name, method, self, options).render
|
731
819
|
end
|
732
820
|
|
733
821
|
# Returns a file upload input tag tailored for accessing a specified attribute (identified by +method+) on an object
|
@@ -737,18 +825,29 @@ module ActionView
|
|
737
825
|
#
|
738
826
|
# Using this method inside a +form_for+ block will set the enclosing form's encoding to <tt>multipart/form-data</tt>.
|
739
827
|
#
|
828
|
+
# ==== Options
|
829
|
+
# * Creates standard HTML attributes for the tag.
|
830
|
+
# * <tt>:disabled</tt> - If set to true, the user will not be able to use this input.
|
831
|
+
# * <tt>:multiple</tt> - If set to true, *in most updated browsers* the user will be allowed to select multiple files.
|
832
|
+
# * <tt>:accept</tt> - If set to one or multiple mime-types, the user will be suggested a filter when choosing a file. You still need to set up model validations.
|
833
|
+
#
|
740
834
|
# ==== Examples
|
741
835
|
# file_field(:user, :avatar)
|
742
836
|
# # => <input type="file" id="user_avatar" name="user[avatar]" />
|
743
837
|
#
|
744
|
-
# file_field(:post, :
|
838
|
+
# file_field(:post, :image, :multiple => true)
|
839
|
+
# # => <input type="file" id="post_image" name="post[image]" multiple="true" />
|
840
|
+
#
|
841
|
+
# file_field(:post, :attached, accept: 'text/html')
|
745
842
|
# # => <input accept="text/html" type="file" id="post_attached" name="post[attached]" />
|
746
843
|
#
|
747
|
-
# file_field(:
|
748
|
-
# # => <input type="file" id="
|
844
|
+
# file_field(:post, :image, accept: 'image/png,image/gif,image/jpeg')
|
845
|
+
# # => <input type="file" id="post_image" name="post[image]" accept="image/png,image/gif,image/jpeg" />
|
749
846
|
#
|
847
|
+
# file_field(:attachment, :file, class: 'file_input')
|
848
|
+
# # => <input type="file" id="attachment_file" name="attachment[file]" class="file_input" />
|
750
849
|
def file_field(object_name, method, options = {})
|
751
|
-
|
850
|
+
Tags::FileField.new(object_name, method, self, options).render
|
752
851
|
end
|
753
852
|
|
754
853
|
# Returns a textarea opening and closing tag set tailored for accessing a specified attribute (identified by +method+)
|
@@ -756,27 +855,27 @@ module ActionView
|
|
756
855
|
# hash with +options+.
|
757
856
|
#
|
758
857
|
# ==== Examples
|
759
|
-
# text_area(:post, :body, :
|
858
|
+
# text_area(:post, :body, cols: 20, rows: 40)
|
760
859
|
# # => <textarea cols="20" rows="40" id="post_body" name="post[body]">
|
761
860
|
# # #{@post.body}
|
762
861
|
# # </textarea>
|
763
862
|
#
|
764
|
-
# text_area(:comment, :text, :
|
863
|
+
# text_area(:comment, :text, size: "20x30")
|
765
864
|
# # => <textarea cols="20" rows="30" id="comment_text" name="comment[text]">
|
766
865
|
# # #{@comment.text}
|
767
866
|
# # </textarea>
|
768
867
|
#
|
769
|
-
# text_area(:application, :notes, :
|
868
|
+
# text_area(:application, :notes, cols: 40, rows: 15, class: 'app_input')
|
770
869
|
# # => <textarea cols="40" rows="15" id="application_notes" name="application[notes]" class="app_input">
|
771
870
|
# # #{@application.notes}
|
772
871
|
# # </textarea>
|
773
872
|
#
|
774
|
-
# text_area(:entry, :body, :
|
873
|
+
# text_area(:entry, :body, size: "20x20", disabled: 'disabled')
|
775
874
|
# # => <textarea cols="20" rows="20" id="entry_body" name="entry[body]" disabled="disabled">
|
776
875
|
# # #{@entry.body}
|
777
876
|
# # </textarea>
|
778
877
|
def text_area(object_name, method, options = {})
|
779
|
-
|
878
|
+
Tags::TextArea.new(object_name, method, self, options).render
|
780
879
|
end
|
781
880
|
|
782
881
|
# Returns a checkbox tag tailored for accessing a specified attribute (identified by +method+) on an object
|
@@ -793,7 +892,7 @@ module ActionView
|
|
793
892
|
# invoice the user unchecks its check box, no +paid+ parameter is sent. So,
|
794
893
|
# any mass-assignment idiom like
|
795
894
|
#
|
796
|
-
# @invoice.
|
895
|
+
# @invoice.update(params[:invoice])
|
797
896
|
#
|
798
897
|
# wouldn't update the flag.
|
799
898
|
#
|
@@ -810,7 +909,7 @@ module ActionView
|
|
810
909
|
# Unfortunately that workaround does not work when the check box goes
|
811
910
|
# within an array-like parameter, as in
|
812
911
|
#
|
813
|
-
# <%= fields_for "project[invoice_attributes][]", invoice, :
|
912
|
+
# <%= fields_for "project[invoice_attributes][]", invoice, index: nil do |form| %>
|
814
913
|
# <%= form.check_box :paid %>
|
815
914
|
# ...
|
816
915
|
# <% end %>
|
@@ -822,33 +921,30 @@ module ActionView
|
|
822
921
|
# In that case it is preferable to either use +check_box_tag+ or to use
|
823
922
|
# hashes instead of arrays.
|
824
923
|
#
|
825
|
-
# ==== Examples
|
826
924
|
# # Let's say that @post.validated? is 1:
|
827
925
|
# check_box("post", "validated")
|
828
926
|
# # => <input name="post[validated]" type="hidden" value="0" />
|
829
|
-
# # <input type="checkbox" id="post_validated" name="post[validated]" value="1" />
|
927
|
+
# # <input checked="checked" type="checkbox" id="post_validated" name="post[validated]" value="1" />
|
830
928
|
#
|
831
929
|
# # Let's say that @puppy.gooddog is "no":
|
832
930
|
# check_box("puppy", "gooddog", {}, "yes", "no")
|
833
931
|
# # => <input name="puppy[gooddog]" type="hidden" value="no" />
|
834
932
|
# # <input type="checkbox" id="puppy_gooddog" name="puppy[gooddog]" value="yes" />
|
835
933
|
#
|
836
|
-
# check_box("eula", "accepted", { :
|
934
|
+
# check_box("eula", "accepted", { class: 'eula_check' }, "yes", "no")
|
837
935
|
# # => <input name="eula[accepted]" type="hidden" value="no" />
|
838
936
|
# # <input type="checkbox" class="eula_check" id="eula_accepted" name="eula[accepted]" value="yes" />
|
839
|
-
#
|
840
937
|
def check_box(object_name, method, options = {}, checked_value = "1", unchecked_value = "0")
|
841
|
-
|
938
|
+
Tags::CheckBox.new(object_name, method, self, checked_value, unchecked_value, options).render
|
842
939
|
end
|
843
940
|
|
844
941
|
# Returns a radio button tag for accessing a specified attribute (identified by +method+) on an object
|
845
942
|
# assigned to the template (identified by +object+). If the current value of +method+ is +tag_value+ the
|
846
943
|
# radio button will be checked.
|
847
944
|
#
|
848
|
-
# To force the radio button to be checked pass <tt
|
945
|
+
# To force the radio button to be checked pass <tt>checked: true</tt> in the
|
849
946
|
# +options+ hash. You may pass HTML options there as well.
|
850
947
|
#
|
851
|
-
# ==== Examples
|
852
948
|
# # Let's say that @post.category returns "rails":
|
853
949
|
# radio_button("post", "category", "rails")
|
854
950
|
# radio_button("post", "category", "java")
|
@@ -860,74 +956,170 @@ module ActionView
|
|
860
956
|
# # => <input type="radio" id="user_receive_newsletter_yes" name="user[receive_newsletter]" value="yes" />
|
861
957
|
# # <input type="radio" id="user_receive_newsletter_no" name="user[receive_newsletter]" value="no" checked="checked" />
|
862
958
|
def radio_button(object_name, method, tag_value, options = {})
|
863
|
-
|
959
|
+
Tags::RadioButton.new(object_name, method, self, tag_value, options).render
|
960
|
+
end
|
961
|
+
|
962
|
+
# Returns a text_field of type "color".
|
963
|
+
#
|
964
|
+
# color_field("car", "color")
|
965
|
+
# # => <input id="car_color" name="car[color]" type="color" value="#000000" />
|
966
|
+
def color_field(object_name, method, options = {})
|
967
|
+
Tags::ColorField.new(object_name, method, self, options).render
|
864
968
|
end
|
865
969
|
|
866
970
|
# Returns an input of type "search" for accessing a specified attribute (identified by +method+) on an object
|
867
971
|
# assigned to the template (identified by +object_name+). Inputs of type "search" may be styled differently by
|
868
972
|
# some browsers.
|
869
973
|
#
|
870
|
-
# ==== Examples
|
871
|
-
#
|
872
974
|
# search_field(:user, :name)
|
873
|
-
# # => <input id="user_name" name="user[name]"
|
874
|
-
# search_field(:user, :name, :
|
875
|
-
# # => <input autosave="false" id="user_name" name="user[name]"
|
876
|
-
# search_field(:user, :name, :
|
877
|
-
# # => <input id="user_name" name="user[name]" results="3"
|
975
|
+
# # => <input id="user_name" name="user[name]" type="search" />
|
976
|
+
# search_field(:user, :name, autosave: false)
|
977
|
+
# # => <input autosave="false" id="user_name" name="user[name]" type="search" />
|
978
|
+
# search_field(:user, :name, results: 3)
|
979
|
+
# # => <input id="user_name" name="user[name]" results="3" type="search" />
|
878
980
|
# # Assume request.host returns "www.example.com"
|
879
|
-
# search_field(:user, :name, :
|
880
|
-
# # => <input autosave="com.example.www" id="user_name" name="user[name]" results="10"
|
881
|
-
# search_field(:user, :name, :
|
882
|
-
# # => <input id="user_name" incremental="true" name="user[name]" onsearch="true"
|
883
|
-
# search_field(:user, :name, :
|
884
|
-
# # => <input autosave="false" id="user_name" incremental="true" name="user[name]" onsearch="true"
|
885
|
-
# search_field(:user, :name, :
|
886
|
-
# # => <input autosave="com.example.www" id="user_name" incremental="true" name="user[name]" onsearch="true" results="10"
|
887
|
-
#
|
981
|
+
# search_field(:user, :name, autosave: true)
|
982
|
+
# # => <input autosave="com.example.www" id="user_name" name="user[name]" results="10" type="search" />
|
983
|
+
# search_field(:user, :name, onsearch: true)
|
984
|
+
# # => <input id="user_name" incremental="true" name="user[name]" onsearch="true" type="search" />
|
985
|
+
# search_field(:user, :name, autosave: false, onsearch: true)
|
986
|
+
# # => <input autosave="false" id="user_name" incremental="true" name="user[name]" onsearch="true" type="search" />
|
987
|
+
# search_field(:user, :name, autosave: true, onsearch: true)
|
988
|
+
# # => <input autosave="com.example.www" id="user_name" incremental="true" name="user[name]" onsearch="true" results="10" type="search" />
|
888
989
|
def search_field(object_name, method, options = {})
|
889
|
-
|
890
|
-
|
891
|
-
if options["autosave"]
|
892
|
-
if options["autosave"] == true
|
893
|
-
options["autosave"] = request.host.split(".").reverse.join(".")
|
894
|
-
end
|
895
|
-
options["results"] ||= 10
|
896
|
-
end
|
897
|
-
|
898
|
-
if options["onsearch"]
|
899
|
-
options["incremental"] = true unless options.has_key?("incremental")
|
900
|
-
end
|
901
|
-
|
902
|
-
InstanceTag.new(object_name, method, self, options.delete("object")).to_input_field_tag("search", options)
|
990
|
+
Tags::SearchField.new(object_name, method, self, options).render
|
903
991
|
end
|
904
992
|
|
905
993
|
# Returns a text_field of type "tel".
|
906
994
|
#
|
907
995
|
# telephone_field("user", "phone")
|
908
|
-
# # => <input id="user_phone" name="user[phone]"
|
996
|
+
# # => <input id="user_phone" name="user[phone]" type="tel" />
|
909
997
|
#
|
910
998
|
def telephone_field(object_name, method, options = {})
|
911
|
-
|
999
|
+
Tags::TelField.new(object_name, method, self, options).render
|
912
1000
|
end
|
1001
|
+
# aliases telephone_field
|
913
1002
|
alias phone_field telephone_field
|
914
1003
|
|
1004
|
+
# Returns a text_field of type "date".
|
1005
|
+
#
|
1006
|
+
# date_field("user", "born_on")
|
1007
|
+
# # => <input id="user_born_on" name="user[born_on]" type="date" />
|
1008
|
+
#
|
1009
|
+
# The default value is generated by trying to call "to_date"
|
1010
|
+
# on the object's value, which makes it behave as expected for instances
|
1011
|
+
# of DateTime and ActiveSupport::TimeWithZone. You can still override that
|
1012
|
+
# by passing the "value" option explicitly, e.g.
|
1013
|
+
#
|
1014
|
+
# @user.born_on = Date.new(1984, 1, 27)
|
1015
|
+
# date_field("user", "born_on", value: "1984-05-12")
|
1016
|
+
# # => <input id="user_born_on" name="user[born_on]" type="date" value="1984-05-12" />
|
1017
|
+
#
|
1018
|
+
def date_field(object_name, method, options = {})
|
1019
|
+
Tags::DateField.new(object_name, method, self, options).render
|
1020
|
+
end
|
1021
|
+
|
1022
|
+
# Returns a text_field of type "time".
|
1023
|
+
#
|
1024
|
+
# The default value is generated by trying to call +strftime+ with "%T.%L"
|
1025
|
+
# on the objects's value. It is still possible to override that
|
1026
|
+
# by passing the "value" option.
|
1027
|
+
#
|
1028
|
+
# === Options
|
1029
|
+
# * Accepts same options as time_field_tag
|
1030
|
+
#
|
1031
|
+
# === Example
|
1032
|
+
# time_field("task", "started_at")
|
1033
|
+
# # => <input id="task_started_at" name="task[started_at]" type="time" />
|
1034
|
+
#
|
1035
|
+
def time_field(object_name, method, options = {})
|
1036
|
+
Tags::TimeField.new(object_name, method, self, options).render
|
1037
|
+
end
|
1038
|
+
|
1039
|
+
# Returns a text_field of type "datetime".
|
1040
|
+
#
|
1041
|
+
# datetime_field("user", "born_on")
|
1042
|
+
# # => <input id="user_born_on" name="user[born_on]" type="datetime" />
|
1043
|
+
#
|
1044
|
+
# The default value is generated by trying to call +strftime+ with "%Y-%m-%dT%T.%L%z"
|
1045
|
+
# on the object's value, which makes it behave as expected for instances
|
1046
|
+
# of DateTime and ActiveSupport::TimeWithZone.
|
1047
|
+
#
|
1048
|
+
# @user.born_on = Date.new(1984, 1, 12)
|
1049
|
+
# datetime_field("user", "born_on")
|
1050
|
+
# # => <input id="user_born_on" name="user[born_on]" type="datetime" value="1984-01-12T00:00:00.000+0000" />
|
1051
|
+
#
|
1052
|
+
def datetime_field(object_name, method, options = {})
|
1053
|
+
Tags::DatetimeField.new(object_name, method, self, options).render
|
1054
|
+
end
|
1055
|
+
|
1056
|
+
# Returns a text_field of type "datetime-local".
|
1057
|
+
#
|
1058
|
+
# datetime_local_field("user", "born_on")
|
1059
|
+
# # => <input id="user_born_on" name="user[born_on]" type="datetime-local" />
|
1060
|
+
#
|
1061
|
+
# The default value is generated by trying to call +strftime+ with "%Y-%m-%dT%T"
|
1062
|
+
# on the object's value, which makes it behave as expected for instances
|
1063
|
+
# of DateTime and ActiveSupport::TimeWithZone.
|
1064
|
+
#
|
1065
|
+
# @user.born_on = Date.new(1984, 1, 12)
|
1066
|
+
# datetime_local_field("user", "born_on")
|
1067
|
+
# # => <input id="user_born_on" name="user[born_on]" type="datetime-local" value="1984-01-12T00:00:00" />
|
1068
|
+
#
|
1069
|
+
def datetime_local_field(object_name, method, options = {})
|
1070
|
+
Tags::DatetimeLocalField.new(object_name, method, self, options).render
|
1071
|
+
end
|
1072
|
+
|
1073
|
+
# Returns a text_field of type "month".
|
1074
|
+
#
|
1075
|
+
# month_field("user", "born_on")
|
1076
|
+
# # => <input id="user_born_on" name="user[born_on]" type="month" />
|
1077
|
+
#
|
1078
|
+
# The default value is generated by trying to call +strftime+ with "%Y-%m"
|
1079
|
+
# on the object's value, which makes it behave as expected for instances
|
1080
|
+
# of DateTime and ActiveSupport::TimeWithZone.
|
1081
|
+
#
|
1082
|
+
# @user.born_on = Date.new(1984, 1, 27)
|
1083
|
+
# month_field("user", "born_on")
|
1084
|
+
# # => <input id="user_born_on" name="user[born_on]" type="date" value="1984-01" />
|
1085
|
+
#
|
1086
|
+
def month_field(object_name, method, options = {})
|
1087
|
+
Tags::MonthField.new(object_name, method, self, options).render
|
1088
|
+
end
|
1089
|
+
|
1090
|
+
# Returns a text_field of type "week".
|
1091
|
+
#
|
1092
|
+
# week_field("user", "born_on")
|
1093
|
+
# # => <input id="user_born_on" name="user[born_on]" type="week" />
|
1094
|
+
#
|
1095
|
+
# The default value is generated by trying to call +strftime+ with "%Y-W%W"
|
1096
|
+
# on the object's value, which makes it behave as expected for instances
|
1097
|
+
# of DateTime and ActiveSupport::TimeWithZone.
|
1098
|
+
#
|
1099
|
+
# @user.born_on = Date.new(1984, 5, 12)
|
1100
|
+
# week_field("user", "born_on")
|
1101
|
+
# # => <input id="user_born_on" name="user[born_on]" type="date" value="1984-W19" />
|
1102
|
+
#
|
1103
|
+
def week_field(object_name, method, options = {})
|
1104
|
+
Tags::WeekField.new(object_name, method, self, options).render
|
1105
|
+
end
|
1106
|
+
|
915
1107
|
# Returns a text_field of type "url".
|
916
1108
|
#
|
917
1109
|
# url_field("user", "homepage")
|
918
|
-
# # => <input id="user_homepage"
|
1110
|
+
# # => <input id="user_homepage" name="user[homepage]" type="url" />
|
919
1111
|
#
|
920
1112
|
def url_field(object_name, method, options = {})
|
921
|
-
|
1113
|
+
Tags::UrlField.new(object_name, method, self, options).render
|
922
1114
|
end
|
923
1115
|
|
924
1116
|
# Returns a text_field of type "email".
|
925
1117
|
#
|
926
1118
|
# email_field("user", "address")
|
927
|
-
# # => <input id="user_address"
|
1119
|
+
# # => <input id="user_address" name="user[address]" type="email" />
|
928
1120
|
#
|
929
1121
|
def email_field(object_name, method, options = {})
|
930
|
-
|
1122
|
+
Tags::EmailField.new(object_name, method, self, options).render
|
931
1123
|
end
|
932
1124
|
|
933
1125
|
# Returns an input tag of type "number".
|
@@ -935,7 +1127,7 @@ module ActionView
|
|
935
1127
|
# ==== Options
|
936
1128
|
# * Accepts same options as number_field_tag
|
937
1129
|
def number_field(object_name, method, options = {})
|
938
|
-
|
1130
|
+
Tags::NumberField.new(object_name, method, self, options).render
|
939
1131
|
end
|
940
1132
|
|
941
1133
|
# Returns an input tag of type "range".
|
@@ -943,23 +1135,23 @@ module ActionView
|
|
943
1135
|
# ==== Options
|
944
1136
|
# * Accepts same options as range_field_tag
|
945
1137
|
def range_field(object_name, method, options = {})
|
946
|
-
|
1138
|
+
Tags::RangeField.new(object_name, method, self, options).render
|
947
1139
|
end
|
948
1140
|
|
949
1141
|
private
|
950
1142
|
|
951
|
-
def instantiate_builder(record_name, record_object, options
|
1143
|
+
def instantiate_builder(record_name, record_object, options)
|
952
1144
|
case record_name
|
953
1145
|
when String, Symbol
|
954
1146
|
object = record_object
|
955
1147
|
object_name = record_name
|
956
1148
|
else
|
957
1149
|
object = record_name
|
958
|
-
object_name =
|
1150
|
+
object_name = model_name_from_record_or_class(object).param_key
|
959
1151
|
end
|
960
1152
|
|
961
1153
|
builder = options[:builder] || default_form_builder
|
962
|
-
builder.new(object_name, object, self, options
|
1154
|
+
builder.new(object_name, object, self, options)
|
963
1155
|
end
|
964
1156
|
|
965
1157
|
def default_form_builder
|
@@ -968,286 +1160,24 @@ module ActionView
|
|
968
1160
|
end
|
969
1161
|
end
|
970
1162
|
|
971
|
-
class InstanceTag
|
972
|
-
include Helpers::ActiveModelInstanceTag, Helpers::TagHelper, Helpers::FormTagHelper
|
973
|
-
|
974
|
-
attr_reader :object, :method_name, :object_name
|
975
|
-
|
976
|
-
DEFAULT_FIELD_OPTIONS = { "size" => 30 }
|
977
|
-
DEFAULT_RADIO_OPTIONS = { }
|
978
|
-
DEFAULT_TEXT_AREA_OPTIONS = { "cols" => 40, "rows" => 20 }
|
979
|
-
|
980
|
-
def initialize(object_name, method_name, template_object, object = nil)
|
981
|
-
@object_name, @method_name = object_name.to_s.dup, method_name.to_s.dup
|
982
|
-
@template_object = template_object
|
983
|
-
|
984
|
-
@object_name.sub!(/\[\]$/,"") || @object_name.sub!(/\[\]\]$/,"]")
|
985
|
-
@object = retrieve_object(object)
|
986
|
-
@auto_index = retrieve_autoindex(Regexp.last_match.pre_match) if Regexp.last_match
|
987
|
-
end
|
988
|
-
|
989
|
-
def to_label_tag(text = nil, options = {}, &block)
|
990
|
-
options = options.stringify_keys
|
991
|
-
tag_value = options.delete("value")
|
992
|
-
name_and_id = options.dup
|
993
|
-
|
994
|
-
if name_and_id["for"]
|
995
|
-
name_and_id["id"] = name_and_id["for"]
|
996
|
-
else
|
997
|
-
name_and_id.delete("id")
|
998
|
-
end
|
999
|
-
|
1000
|
-
add_default_name_and_id_for_value(tag_value, name_and_id)
|
1001
|
-
options.delete("index")
|
1002
|
-
options.delete("namespace")
|
1003
|
-
options["for"] ||= name_and_id["id"]
|
1004
|
-
|
1005
|
-
if block_given?
|
1006
|
-
@template_object.label_tag(name_and_id["id"], options, &block)
|
1007
|
-
else
|
1008
|
-
content = if text.blank?
|
1009
|
-
object_name.gsub!(/\[(.*)_attributes\]\[\d\]/, '.\1')
|
1010
|
-
method_and_value = tag_value.present? ? "#{method_name}.#{tag_value}" : method_name
|
1011
|
-
|
1012
|
-
if object.respond_to?(:to_model)
|
1013
|
-
key = object.class.model_name.i18n_key
|
1014
|
-
i18n_default = ["#{key}.#{method_and_value}".to_sym, ""]
|
1015
|
-
end
|
1016
|
-
|
1017
|
-
i18n_default ||= ""
|
1018
|
-
I18n.t("#{object_name}.#{method_and_value}", :default => i18n_default, :scope => "helpers.label").presence
|
1019
|
-
else
|
1020
|
-
text.to_s
|
1021
|
-
end
|
1022
|
-
|
1023
|
-
content ||= if object && object.class.respond_to?(:human_attribute_name)
|
1024
|
-
object.class.human_attribute_name(method_name)
|
1025
|
-
end
|
1026
|
-
|
1027
|
-
content ||= method_name.humanize
|
1028
|
-
|
1029
|
-
label_tag(name_and_id["id"], content, options)
|
1030
|
-
end
|
1031
|
-
end
|
1032
|
-
|
1033
|
-
def to_input_field_tag(field_type, options = {})
|
1034
|
-
options = options.stringify_keys
|
1035
|
-
options["size"] = options["maxlength"] || DEFAULT_FIELD_OPTIONS["size"] unless options.key?("size")
|
1036
|
-
options = DEFAULT_FIELD_OPTIONS.merge(options)
|
1037
|
-
if field_type == "hidden"
|
1038
|
-
options.delete("size")
|
1039
|
-
end
|
1040
|
-
options["type"] ||= field_type
|
1041
|
-
options["value"] = options.fetch("value"){ value_before_type_cast(object) } unless field_type == "file"
|
1042
|
-
options["value"] &&= ERB::Util.html_escape(options["value"])
|
1043
|
-
add_default_name_and_id(options)
|
1044
|
-
tag("input", options)
|
1045
|
-
end
|
1046
|
-
|
1047
|
-
def to_number_field_tag(field_type, options = {})
|
1048
|
-
options = options.stringify_keys
|
1049
|
-
options['size'] ||= nil
|
1050
|
-
|
1051
|
-
if range = options.delete("in") || options.delete("within")
|
1052
|
-
options.update("min" => range.min, "max" => range.max)
|
1053
|
-
end
|
1054
|
-
to_input_field_tag(field_type, options)
|
1055
|
-
end
|
1056
|
-
|
1057
|
-
def to_radio_button_tag(tag_value, options = {})
|
1058
|
-
options = DEFAULT_RADIO_OPTIONS.merge(options.stringify_keys)
|
1059
|
-
options["type"] = "radio"
|
1060
|
-
options["value"] = tag_value
|
1061
|
-
if options.has_key?("checked")
|
1062
|
-
cv = options.delete "checked"
|
1063
|
-
checked = cv == true || cv == "checked"
|
1064
|
-
else
|
1065
|
-
checked = self.class.radio_button_checked?(value(object), tag_value)
|
1066
|
-
end
|
1067
|
-
options["checked"] = "checked" if checked
|
1068
|
-
add_default_name_and_id_for_value(tag_value, options)
|
1069
|
-
tag("input", options)
|
1070
|
-
end
|
1071
|
-
|
1072
|
-
def to_text_area_tag(options = {})
|
1073
|
-
options = DEFAULT_TEXT_AREA_OPTIONS.merge(options.stringify_keys)
|
1074
|
-
add_default_name_and_id(options)
|
1075
|
-
|
1076
|
-
if size = options.delete("size")
|
1077
|
-
options["cols"], options["rows"] = size.split("x") if size.respond_to?(:split)
|
1078
|
-
end
|
1079
|
-
|
1080
|
-
content_tag("textarea", options.delete('value') || value_before_type_cast(object), options)
|
1081
|
-
end
|
1082
|
-
|
1083
|
-
def to_check_box_tag(options = {}, checked_value = "1", unchecked_value = "0")
|
1084
|
-
options = options.stringify_keys
|
1085
|
-
options["type"] = "checkbox"
|
1086
|
-
options["value"] = checked_value
|
1087
|
-
if options.has_key?("checked")
|
1088
|
-
cv = options.delete "checked"
|
1089
|
-
checked = cv == true || cv == "checked"
|
1090
|
-
else
|
1091
|
-
checked = self.class.check_box_checked?(value(object), checked_value)
|
1092
|
-
end
|
1093
|
-
options["checked"] = "checked" if checked
|
1094
|
-
if options["multiple"]
|
1095
|
-
add_default_name_and_id_for_value(checked_value, options)
|
1096
|
-
options.delete("multiple")
|
1097
|
-
else
|
1098
|
-
add_default_name_and_id(options)
|
1099
|
-
end
|
1100
|
-
hidden = unchecked_value ? tag("input", "name" => options["name"], "type" => "hidden", "value" => unchecked_value, "disabled" => options["disabled"]) : ""
|
1101
|
-
checkbox = tag("input", options)
|
1102
|
-
(hidden + checkbox).html_safe
|
1103
|
-
end
|
1104
|
-
|
1105
|
-
def to_boolean_select_tag(options = {})
|
1106
|
-
options = options.stringify_keys
|
1107
|
-
add_default_name_and_id(options)
|
1108
|
-
value = value(object)
|
1109
|
-
tag_text = "<select"
|
1110
|
-
tag_text << tag_options(options)
|
1111
|
-
tag_text << "><option value=\"false\""
|
1112
|
-
tag_text << " selected" if value == false
|
1113
|
-
tag_text << ">False</option><option value=\"true\""
|
1114
|
-
tag_text << " selected" if value
|
1115
|
-
tag_text << ">True</option></select>"
|
1116
|
-
end
|
1117
|
-
|
1118
|
-
def to_content_tag(tag_name, options = {})
|
1119
|
-
content_tag(tag_name, value(object), options)
|
1120
|
-
end
|
1121
|
-
|
1122
|
-
def retrieve_object(object)
|
1123
|
-
if object
|
1124
|
-
object
|
1125
|
-
elsif @template_object.instance_variable_defined?("@#{@object_name}")
|
1126
|
-
@template_object.instance_variable_get("@#{@object_name}")
|
1127
|
-
end
|
1128
|
-
rescue NameError
|
1129
|
-
# As @object_name may contain the nested syntax (item[subobject]) we need to fallback to nil.
|
1130
|
-
nil
|
1131
|
-
end
|
1132
|
-
|
1133
|
-
def retrieve_autoindex(pre_match)
|
1134
|
-
object = self.object || @template_object.instance_variable_get("@#{pre_match}")
|
1135
|
-
if object && object.respond_to?(:to_param)
|
1136
|
-
object.to_param
|
1137
|
-
else
|
1138
|
-
raise ArgumentError, "object[] naming but object param and @object var don't exist or don't respond to to_param: #{object.inspect}"
|
1139
|
-
end
|
1140
|
-
end
|
1141
|
-
|
1142
|
-
def value(object)
|
1143
|
-
self.class.value(object, @method_name)
|
1144
|
-
end
|
1145
|
-
|
1146
|
-
def value_before_type_cast(object)
|
1147
|
-
self.class.value_before_type_cast(object, @method_name)
|
1148
|
-
end
|
1149
|
-
|
1150
|
-
class << self
|
1151
|
-
def value(object, method_name)
|
1152
|
-
object.send method_name if object
|
1153
|
-
end
|
1154
|
-
|
1155
|
-
def value_before_type_cast(object, method_name)
|
1156
|
-
unless object.nil?
|
1157
|
-
object.respond_to?(method_name + "_before_type_cast") ?
|
1158
|
-
object.send(method_name + "_before_type_cast") :
|
1159
|
-
object.send(method_name)
|
1160
|
-
end
|
1161
|
-
end
|
1162
|
-
|
1163
|
-
def check_box_checked?(value, checked_value)
|
1164
|
-
case value
|
1165
|
-
when TrueClass, FalseClass
|
1166
|
-
value
|
1167
|
-
when NilClass
|
1168
|
-
false
|
1169
|
-
when Integer
|
1170
|
-
value != 0
|
1171
|
-
when String
|
1172
|
-
value == checked_value
|
1173
|
-
when Array
|
1174
|
-
value.include?(checked_value)
|
1175
|
-
else
|
1176
|
-
value.to_i != 0
|
1177
|
-
end
|
1178
|
-
end
|
1179
|
-
|
1180
|
-
def radio_button_checked?(value, checked_value)
|
1181
|
-
value.to_s == checked_value.to_s
|
1182
|
-
end
|
1183
|
-
end
|
1184
|
-
|
1185
|
-
private
|
1186
|
-
def add_default_name_and_id_for_value(tag_value, options)
|
1187
|
-
unless tag_value.nil?
|
1188
|
-
pretty_tag_value = tag_value.to_s.gsub(/\s/, "_").gsub(/[^-\w]/, "").downcase
|
1189
|
-
specified_id = options["id"]
|
1190
|
-
add_default_name_and_id(options)
|
1191
|
-
options["id"] += "_#{pretty_tag_value}" if specified_id.blank? && options["id"].present?
|
1192
|
-
else
|
1193
|
-
add_default_name_and_id(options)
|
1194
|
-
end
|
1195
|
-
end
|
1196
|
-
|
1197
|
-
def add_default_name_and_id(options)
|
1198
|
-
if options.has_key?("index")
|
1199
|
-
options["name"] ||= tag_name_with_index(options["index"], options["multiple"])
|
1200
|
-
options["id"] = options.fetch("id"){ tag_id_with_index(options["index"]) }
|
1201
|
-
options.delete("index")
|
1202
|
-
elsif defined?(@auto_index)
|
1203
|
-
options["name"] ||= tag_name_with_index(@auto_index, options["multiple"])
|
1204
|
-
options["id"] = options.fetch("id"){ tag_id_with_index(@auto_index) }
|
1205
|
-
else
|
1206
|
-
options["name"] ||= tag_name(options["multiple"])
|
1207
|
-
options["id"] = options.fetch("id"){ tag_id }
|
1208
|
-
end
|
1209
|
-
|
1210
|
-
options["id"] = [options.delete('namespace'), options["id"]].compact.join("_").presence
|
1211
|
-
end
|
1212
|
-
|
1213
|
-
def tag_name(multiple = false)
|
1214
|
-
"#{@object_name}[#{sanitized_method_name}]#{"[]" if multiple}"
|
1215
|
-
end
|
1216
|
-
|
1217
|
-
def tag_name_with_index(index, multiple = false)
|
1218
|
-
"#{@object_name}[#{index}][#{sanitized_method_name}]#{"[]" if multiple}"
|
1219
|
-
end
|
1220
|
-
|
1221
|
-
def tag_id
|
1222
|
-
"#{sanitized_object_name}_#{sanitized_method_name}"
|
1223
|
-
end
|
1224
|
-
|
1225
|
-
def tag_id_with_index(index)
|
1226
|
-
"#{sanitized_object_name}_#{index}_#{sanitized_method_name}"
|
1227
|
-
end
|
1228
|
-
|
1229
|
-
def sanitized_object_name
|
1230
|
-
@sanitized_object_name ||= @object_name.gsub(/\]\[|[^-a-zA-Z0-9:.]/, "_").sub(/_$/, "")
|
1231
|
-
end
|
1232
|
-
|
1233
|
-
def sanitized_method_name
|
1234
|
-
@sanitized_method_name ||= @method_name.sub(/\?$/,"")
|
1235
|
-
end
|
1236
|
-
end
|
1237
|
-
|
1238
1163
|
class FormBuilder
|
1164
|
+
include ModelNaming
|
1165
|
+
|
1239
1166
|
# The methods which wrap a form helper call.
|
1240
1167
|
class_attribute :field_helpers
|
1241
|
-
self.field_helpers = FormHelper.
|
1168
|
+
self.field_helpers = FormHelper.instance_methods - [:form_for, :convert_to_model, :model_name_from_record_or_class]
|
1242
1169
|
|
1243
1170
|
attr_accessor :object_name, :object, :options
|
1244
1171
|
|
1245
|
-
attr_reader :multipart, :
|
1172
|
+
attr_reader :multipart, :index
|
1246
1173
|
alias :multipart? :multipart
|
1247
1174
|
|
1248
1175
|
def multipart=(multipart)
|
1249
1176
|
@multipart = multipart
|
1250
|
-
|
1177
|
+
|
1178
|
+
if parent_builder = @options[:parent_builder]
|
1179
|
+
parent_builder.multipart = multipart
|
1180
|
+
end
|
1251
1181
|
end
|
1252
1182
|
|
1253
1183
|
def self._to_partial_path
|
@@ -1262,10 +1192,13 @@ module ActionView
|
|
1262
1192
|
self
|
1263
1193
|
end
|
1264
1194
|
|
1265
|
-
def initialize(object_name, object, template, options,
|
1195
|
+
def initialize(object_name, object, template, options, block=nil)
|
1196
|
+
if block
|
1197
|
+
ActiveSupport::Deprecation.warn "Giving a block to FormBuilder is deprecated and has no effect anymore."
|
1198
|
+
end
|
1199
|
+
|
1266
1200
|
@nested_child_index = {}
|
1267
|
-
@object_name, @object, @template, @options
|
1268
|
-
@parent_builder = options[:parent_builder]
|
1201
|
+
@object_name, @object, @template, @options = object_name, object, template, options
|
1269
1202
|
@default_options = @options ? @options.slice(:index, :namespace) : {}
|
1270
1203
|
if @object_name.to_s.match(/\[\]$/)
|
1271
1204
|
if object ||= @template.instance_variable_get("@#{Regexp.last_match.pre_match}") and object.respond_to?(:to_param)
|
@@ -1275,9 +1208,10 @@ module ActionView
|
|
1275
1208
|
end
|
1276
1209
|
end
|
1277
1210
|
@multipart = nil
|
1211
|
+
@index = options[:index] || options[:child_index]
|
1278
1212
|
end
|
1279
1213
|
|
1280
|
-
(field_helpers -
|
1214
|
+
(field_helpers - [:label, :check_box, :radio_button, :fields_for, :hidden_field, :file_field]).each do |selector|
|
1281
1215
|
class_eval <<-RUBY_EVAL, __FILE__, __LINE__ + 1
|
1282
1216
|
def #{selector}(method, options = {}) # def text_field(method, options = {})
|
1283
1217
|
@template.send( # @template.send(
|
@@ -1289,50 +1223,464 @@ module ActionView
|
|
1289
1223
|
RUBY_EVAL
|
1290
1224
|
end
|
1291
1225
|
|
1292
|
-
|
1293
|
-
|
1294
|
-
|
1295
|
-
|
1296
|
-
|
1297
|
-
|
1298
|
-
|
1299
|
-
|
1300
|
-
|
1301
|
-
|
1302
|
-
|
1303
|
-
|
1304
|
-
|
1305
|
-
|
1306
|
-
|
1307
|
-
|
1308
|
-
|
1309
|
-
|
1310
|
-
|
1311
|
-
|
1312
|
-
|
1313
|
-
|
1314
|
-
|
1315
|
-
|
1316
|
-
|
1317
|
-
end
|
1318
|
-
|
1319
|
-
|
1320
|
-
|
1321
|
-
|
1322
|
-
|
1323
|
-
|
1324
|
-
|
1325
|
-
|
1326
|
-
|
1327
|
-
|
1328
|
-
|
1226
|
+
# Creates a scope around a specific model object like form_for, but
|
1227
|
+
# doesn't create the form tags themselves. This makes fields_for suitable
|
1228
|
+
# for specifying additional model objects in the same form.
|
1229
|
+
#
|
1230
|
+
# Although the usage and purpose of +field_for+ is similar to +form_for+'s,
|
1231
|
+
# its method signature is slightly different. Like +form_for+, it yields
|
1232
|
+
# a FormBuilder object associated with a particular model object to a block,
|
1233
|
+
# and within the block allows methods to be called on the builder to
|
1234
|
+
# generate fields associated with the model object. Fields may reflect
|
1235
|
+
# a model object in two ways - how they are named (hence how submitted
|
1236
|
+
# values appear within the +params+ hash in the controller) and what
|
1237
|
+
# default values are shown when the form the fields appear in is first
|
1238
|
+
# displayed. In order for both of these features to be specified independently,
|
1239
|
+
# both an object name (represented by either a symbol or string) and the
|
1240
|
+
# object itself can be passed to the method separately -
|
1241
|
+
#
|
1242
|
+
# <%= form_for @person do |person_form| %>
|
1243
|
+
# First name: <%= person_form.text_field :first_name %>
|
1244
|
+
# Last name : <%= person_form.text_field :last_name %>
|
1245
|
+
#
|
1246
|
+
# <%= fields_for :permission, @person.permission do |permission_fields| %>
|
1247
|
+
# Admin? : <%= permission_fields.check_box :admin %>
|
1248
|
+
# <% end %>
|
1249
|
+
#
|
1250
|
+
# <%= f.submit %>
|
1251
|
+
# <% end %>
|
1252
|
+
#
|
1253
|
+
# In this case, the checkbox field will be represented by an HTML +input+
|
1254
|
+
# tag with the +name+ attribute <tt>permission[admin]</tt>, and the submitted
|
1255
|
+
# value will appear in the controller as <tt>params[:permission][:admin]</tt>.
|
1256
|
+
# If <tt>@person.permission</tt> is an existing record with an attribute
|
1257
|
+
# +admin+, the initial state of the checkbox when first displayed will
|
1258
|
+
# reflect the value of <tt>@person.permission.admin</tt>.
|
1259
|
+
#
|
1260
|
+
# Often this can be simplified by passing just the name of the model
|
1261
|
+
# object to +fields_for+ -
|
1262
|
+
#
|
1263
|
+
# <%= fields_for :permission do |permission_fields| %>
|
1264
|
+
# Admin?: <%= permission_fields.check_box :admin %>
|
1265
|
+
# <% end %>
|
1266
|
+
#
|
1267
|
+
# ...in which case, if <tt>:permission</tt> also happens to be the name of an
|
1268
|
+
# instance variable <tt>@permission</tt>, the initial state of the input
|
1269
|
+
# field will reflect the value of that variable's attribute <tt>@permission.admin</tt>.
|
1270
|
+
#
|
1271
|
+
# Alternatively, you can pass just the model object itself (if the first
|
1272
|
+
# argument isn't a string or symbol +fields_for+ will realize that the
|
1273
|
+
# name has been omitted) -
|
1274
|
+
#
|
1275
|
+
# <%= fields_for @person.permission do |permission_fields| %>
|
1276
|
+
# Admin?: <%= permission_fields.check_box :admin %>
|
1277
|
+
# <% end %>
|
1278
|
+
#
|
1279
|
+
# and +fields_for+ will derive the required name of the field from the
|
1280
|
+
# _class_ of the model object, e.g. if <tt>@person.permission</tt>, is
|
1281
|
+
# of class +Permission+, the field will still be named <tt>permission[admin]</tt>.
|
1282
|
+
#
|
1283
|
+
# Note: This also works for the methods in FormOptionHelper and
|
1284
|
+
# DateHelper that are designed to work with an object as base, like
|
1285
|
+
# FormOptionHelper#collection_select and DateHelper#datetime_select.
|
1286
|
+
#
|
1287
|
+
# === Nested Attributes Examples
|
1288
|
+
#
|
1289
|
+
# When the object belonging to the current scope has a nested attribute
|
1290
|
+
# writer for a certain attribute, fields_for will yield a new scope
|
1291
|
+
# for that attribute. This allows you to create forms that set or change
|
1292
|
+
# the attributes of a parent object and its associations in one go.
|
1293
|
+
#
|
1294
|
+
# Nested attribute writers are normal setter methods named after an
|
1295
|
+
# association. The most common way of defining these writers is either
|
1296
|
+
# with +accepts_nested_attributes_for+ in a model definition or by
|
1297
|
+
# defining a method with the proper name. For example: the attribute
|
1298
|
+
# writer for the association <tt>:address</tt> is called
|
1299
|
+
# <tt>address_attributes=</tt>.
|
1300
|
+
#
|
1301
|
+
# Whether a one-to-one or one-to-many style form builder will be yielded
|
1302
|
+
# depends on whether the normal reader method returns a _single_ object
|
1303
|
+
# or an _array_ of objects.
|
1304
|
+
#
|
1305
|
+
# ==== One-to-one
|
1306
|
+
#
|
1307
|
+
# Consider a Person class which returns a _single_ Address from the
|
1308
|
+
# <tt>address</tt> reader method and responds to the
|
1309
|
+
# <tt>address_attributes=</tt> writer method:
|
1310
|
+
#
|
1311
|
+
# class Person
|
1312
|
+
# def address
|
1313
|
+
# @address
|
1314
|
+
# end
|
1315
|
+
#
|
1316
|
+
# def address_attributes=(attributes)
|
1317
|
+
# # Process the attributes hash
|
1318
|
+
# end
|
1319
|
+
# end
|
1320
|
+
#
|
1321
|
+
# This model can now be used with a nested fields_for, like so:
|
1322
|
+
#
|
1323
|
+
# <%= form_for @person do |person_form| %>
|
1324
|
+
# ...
|
1325
|
+
# <%= person_form.fields_for :address do |address_fields| %>
|
1326
|
+
# Street : <%= address_fields.text_field :street %>
|
1327
|
+
# Zip code: <%= address_fields.text_field :zip_code %>
|
1328
|
+
# <% end %>
|
1329
|
+
# ...
|
1330
|
+
# <% end %>
|
1331
|
+
#
|
1332
|
+
# When address is already an association on a Person you can use
|
1333
|
+
# +accepts_nested_attributes_for+ to define the writer method for you:
|
1334
|
+
#
|
1335
|
+
# class Person < ActiveRecord::Base
|
1336
|
+
# has_one :address
|
1337
|
+
# accepts_nested_attributes_for :address
|
1338
|
+
# end
|
1339
|
+
#
|
1340
|
+
# If you want to destroy the associated model through the form, you have
|
1341
|
+
# to enable it first using the <tt>:allow_destroy</tt> option for
|
1342
|
+
# +accepts_nested_attributes_for+:
|
1343
|
+
#
|
1344
|
+
# class Person < ActiveRecord::Base
|
1345
|
+
# has_one :address
|
1346
|
+
# accepts_nested_attributes_for :address, allow_destroy: true
|
1347
|
+
# end
|
1348
|
+
#
|
1349
|
+
# Now, when you use a form element with the <tt>_destroy</tt> parameter,
|
1350
|
+
# with a value that evaluates to +true+, you will destroy the associated
|
1351
|
+
# model (eg. 1, '1', true, or 'true'):
|
1352
|
+
#
|
1353
|
+
# <%= form_for @person do |person_form| %>
|
1354
|
+
# ...
|
1355
|
+
# <%= person_form.fields_for :address do |address_fields| %>
|
1356
|
+
# ...
|
1357
|
+
# Delete: <%= address_fields.check_box :_destroy %>
|
1358
|
+
# <% end %>
|
1359
|
+
# ...
|
1360
|
+
# <% end %>
|
1361
|
+
#
|
1362
|
+
# ==== One-to-many
|
1363
|
+
#
|
1364
|
+
# Consider a Person class which returns an _array_ of Project instances
|
1365
|
+
# from the <tt>projects</tt> reader method and responds to the
|
1366
|
+
# <tt>projects_attributes=</tt> writer method:
|
1367
|
+
#
|
1368
|
+
# class Person
|
1369
|
+
# def projects
|
1370
|
+
# [@project1, @project2]
|
1371
|
+
# end
|
1372
|
+
#
|
1373
|
+
# def projects_attributes=(attributes)
|
1374
|
+
# # Process the attributes hash
|
1375
|
+
# end
|
1376
|
+
# end
|
1377
|
+
#
|
1378
|
+
# Note that the <tt>projects_attributes=</tt> writer method is in fact
|
1379
|
+
# required for fields_for to correctly identify <tt>:projects</tt> as a
|
1380
|
+
# collection, and the correct indices to be set in the form markup.
|
1381
|
+
#
|
1382
|
+
# When projects is already an association on Person you can use
|
1383
|
+
# +accepts_nested_attributes_for+ to define the writer method for you:
|
1384
|
+
#
|
1385
|
+
# class Person < ActiveRecord::Base
|
1386
|
+
# has_many :projects
|
1387
|
+
# accepts_nested_attributes_for :projects
|
1388
|
+
# end
|
1389
|
+
#
|
1390
|
+
# This model can now be used with a nested fields_for. The block given to
|
1391
|
+
# the nested fields_for call will be repeated for each instance in the
|
1392
|
+
# collection:
|
1393
|
+
#
|
1394
|
+
# <%= form_for @person do |person_form| %>
|
1395
|
+
# ...
|
1396
|
+
# <%= person_form.fields_for :projects do |project_fields| %>
|
1397
|
+
# <% if project_fields.object.active? %>
|
1398
|
+
# Name: <%= project_fields.text_field :name %>
|
1399
|
+
# <% end %>
|
1400
|
+
# <% end %>
|
1401
|
+
# ...
|
1402
|
+
# <% end %>
|
1403
|
+
#
|
1404
|
+
# It's also possible to specify the instance to be used:
|
1405
|
+
#
|
1406
|
+
# <%= form_for @person do |person_form| %>
|
1407
|
+
# ...
|
1408
|
+
# <% @person.projects.each do |project| %>
|
1409
|
+
# <% if project.active? %>
|
1410
|
+
# <%= person_form.fields_for :projects, project do |project_fields| %>
|
1411
|
+
# Name: <%= project_fields.text_field :name %>
|
1412
|
+
# <% end %>
|
1413
|
+
# <% end %>
|
1414
|
+
# <% end %>
|
1415
|
+
# ...
|
1416
|
+
# <% end %>
|
1417
|
+
#
|
1418
|
+
# Or a collection to be used:
|
1419
|
+
#
|
1420
|
+
# <%= form_for @person do |person_form| %>
|
1421
|
+
# ...
|
1422
|
+
# <%= person_form.fields_for :projects, @active_projects do |project_fields| %>
|
1423
|
+
# Name: <%= project_fields.text_field :name %>
|
1424
|
+
# <% end %>
|
1425
|
+
# ...
|
1426
|
+
# <% end %>
|
1427
|
+
#
|
1428
|
+
# When projects is already an association on Person you can use
|
1429
|
+
# +accepts_nested_attributes_for+ to define the writer method for you:
|
1430
|
+
#
|
1431
|
+
# class Person < ActiveRecord::Base
|
1432
|
+
# has_many :projects
|
1433
|
+
# accepts_nested_attributes_for :projects
|
1434
|
+
# end
|
1435
|
+
#
|
1436
|
+
# If you want to destroy any of the associated models through the
|
1437
|
+
# form, you have to enable it first using the <tt>:allow_destroy</tt>
|
1438
|
+
# option for +accepts_nested_attributes_for+:
|
1439
|
+
#
|
1440
|
+
# class Person < ActiveRecord::Base
|
1441
|
+
# has_many :projects
|
1442
|
+
# accepts_nested_attributes_for :projects, allow_destroy: true
|
1443
|
+
# end
|
1444
|
+
#
|
1445
|
+
# This will allow you to specify which models to destroy in the
|
1446
|
+
# attributes hash by adding a form element for the <tt>_destroy</tt>
|
1447
|
+
# parameter with a value that evaluates to +true+
|
1448
|
+
# (eg. 1, '1', true, or 'true'):
|
1449
|
+
#
|
1450
|
+
# <%= form_for @person do |person_form| %>
|
1451
|
+
# ...
|
1452
|
+
# <%= person_form.fields_for :projects do |project_fields| %>
|
1453
|
+
# Delete: <%= project_fields.check_box :_destroy %>
|
1454
|
+
# <% end %>
|
1455
|
+
# ...
|
1456
|
+
# <% end %>
|
1457
|
+
#
|
1458
|
+
# When a collection is used you might want to know the index of each
|
1459
|
+
# object into the array. For this purpose, the <tt>index</tt> method
|
1460
|
+
# is available in the FormBuilder object.
|
1461
|
+
#
|
1462
|
+
# <%= form_for @person do |person_form| %>
|
1463
|
+
# ...
|
1464
|
+
# <%= person_form.fields_for :projects do |project_fields| %>
|
1465
|
+
# Project #<%= project_fields.index %>
|
1466
|
+
# ...
|
1467
|
+
# <% end %>
|
1468
|
+
# ...
|
1469
|
+
# <% end %>
|
1470
|
+
#
|
1471
|
+
# Note that fields_for will automatically generate a hidden field
|
1472
|
+
# to store the ID of the record. There are circumstances where this
|
1473
|
+
# hidden field is not needed and you can pass <tt>hidden_field_id: false</tt>
|
1474
|
+
# to prevent fields_for from rendering it automatically.
|
1475
|
+
def fields_for(record_name, record_object = nil, fields_options = {}, &block)
|
1476
|
+
fields_options, record_object = record_object, nil if record_object.is_a?(Hash) && record_object.extractable_options?
|
1477
|
+
fields_options[:builder] ||= options[:builder]
|
1478
|
+
fields_options[:namespace] = options[:namespace]
|
1479
|
+
fields_options[:parent_builder] = self
|
1480
|
+
|
1481
|
+
case record_name
|
1482
|
+
when String, Symbol
|
1483
|
+
if nested_attributes_association?(record_name)
|
1484
|
+
return fields_for_with_nested_attributes(record_name, record_object, fields_options, block)
|
1485
|
+
end
|
1486
|
+
else
|
1487
|
+
record_object = record_name.is_a?(Array) ? record_name.last : record_name
|
1488
|
+
record_name = model_name_from_record_or_class(record_object).param_key
|
1489
|
+
end
|
1490
|
+
|
1491
|
+
index = if options.has_key?(:index)
|
1492
|
+
options[:index]
|
1493
|
+
elsif defined?(@auto_index)
|
1494
|
+
self.object_name = @object_name.to_s.sub(/\[\]$/,"")
|
1495
|
+
@auto_index
|
1496
|
+
end
|
1497
|
+
|
1498
|
+
record_name = index ? "#{object_name}[#{index}][#{record_name}]" : "#{object_name}[#{record_name}]"
|
1499
|
+
fields_options[:child_index] = index
|
1500
|
+
|
1501
|
+
@template.fields_for(record_name, record_object, fields_options, &block)
|
1502
|
+
end
|
1503
|
+
|
1504
|
+
# Returns a label tag tailored for labelling an input field for a specified attribute (identified by +method+) on an object
|
1505
|
+
# assigned to the template (identified by +object+). The text of label will default to the attribute name unless a translation
|
1506
|
+
# is found in the current I18n locale (through helpers.label.<modelname>.<attribute>) or you specify it explicitly.
|
1507
|
+
# Additional options on the label tag can be passed as a hash with +options+. These options will be tagged
|
1508
|
+
# onto the HTML as an HTML element attribute as in the example shown, except for the <tt>:value</tt> option, which is designed to
|
1509
|
+
# target labels for radio_button tags (where the value is used in the ID of the input tag).
|
1510
|
+
#
|
1511
|
+
# ==== Examples
|
1512
|
+
# label(:post, :title)
|
1513
|
+
# # => <label for="post_title">Title</label>
|
1514
|
+
#
|
1515
|
+
# You can localize your labels based on model and attribute names.
|
1516
|
+
# For example you can define the following in your locale (e.g. en.yml)
|
1517
|
+
#
|
1518
|
+
# helpers:
|
1519
|
+
# label:
|
1520
|
+
# post:
|
1521
|
+
# body: "Write your entire text here"
|
1522
|
+
#
|
1523
|
+
# Which then will result in
|
1524
|
+
#
|
1525
|
+
# label(:post, :body)
|
1526
|
+
# # => <label for="post_body">Write your entire text here</label>
|
1527
|
+
#
|
1528
|
+
# Localization can also be based purely on the translation of the attribute-name
|
1529
|
+
# (if you are using ActiveRecord):
|
1530
|
+
#
|
1531
|
+
# activerecord:
|
1532
|
+
# attributes:
|
1533
|
+
# post:
|
1534
|
+
# cost: "Total cost"
|
1535
|
+
#
|
1536
|
+
# label(:post, :cost)
|
1537
|
+
# # => <label for="post_cost">Total cost</label>
|
1538
|
+
#
|
1539
|
+
# label(:post, :title, "A short title")
|
1540
|
+
# # => <label for="post_title">A short title</label>
|
1541
|
+
#
|
1542
|
+
# label(:post, :title, "A short title", class: "title_label")
|
1543
|
+
# # => <label for="post_title" class="title_label">A short title</label>
|
1544
|
+
#
|
1545
|
+
# label(:post, :privacy, "Public Post", value: "public")
|
1546
|
+
# # => <label for="post_privacy_public">Public Post</label>
|
1547
|
+
#
|
1548
|
+
# label(:post, :terms) do
|
1549
|
+
# 'Accept <a href="/terms">Terms</a>.'.html_safe
|
1550
|
+
# end
|
1551
|
+
def label(method, text = nil, options = {}, &block)
|
1552
|
+
@template.label(@object_name, method, text, objectify_options(options), &block)
|
1329
1553
|
end
|
1330
1554
|
|
1555
|
+
# Returns a checkbox tag tailored for accessing a specified attribute (identified by +method+) on an object
|
1556
|
+
# assigned to the template (identified by +object+). This object must be an instance object (@object) and not a local object.
|
1557
|
+
# It's intended that +method+ returns an integer and if that integer is above zero, then the checkbox is checked.
|
1558
|
+
# Additional options on the input tag can be passed as a hash with +options+. The +checked_value+ defaults to 1
|
1559
|
+
# while the default +unchecked_value+ is set to 0 which is convenient for boolean values.
|
1560
|
+
#
|
1561
|
+
# ==== Gotcha
|
1562
|
+
#
|
1563
|
+
# The HTML specification says unchecked check boxes are not successful, and
|
1564
|
+
# thus web browsers do not send them. Unfortunately this introduces a gotcha:
|
1565
|
+
# if an +Invoice+ model has a +paid+ flag, and in the form that edits a paid
|
1566
|
+
# invoice the user unchecks its check box, no +paid+ parameter is sent. So,
|
1567
|
+
# any mass-assignment idiom like
|
1568
|
+
#
|
1569
|
+
# @invoice.update(params[:invoice])
|
1570
|
+
#
|
1571
|
+
# wouldn't update the flag.
|
1572
|
+
#
|
1573
|
+
# To prevent this the helper generates an auxiliary hidden field before
|
1574
|
+
# the very check box. The hidden field has the same name and its
|
1575
|
+
# attributes mimic an unchecked check box.
|
1576
|
+
#
|
1577
|
+
# This way, the client either sends only the hidden field (representing
|
1578
|
+
# the check box is unchecked), or both fields. Since the HTML specification
|
1579
|
+
# says key/value pairs have to be sent in the same order they appear in the
|
1580
|
+
# form, and parameters extraction gets the last occurrence of any repeated
|
1581
|
+
# key in the query string, that works for ordinary forms.
|
1582
|
+
#
|
1583
|
+
# Unfortunately that workaround does not work when the check box goes
|
1584
|
+
# within an array-like parameter, as in
|
1585
|
+
#
|
1586
|
+
# <%= fields_for "project[invoice_attributes][]", invoice, index: nil do |form| %>
|
1587
|
+
# <%= form.check_box :paid %>
|
1588
|
+
# ...
|
1589
|
+
# <% end %>
|
1590
|
+
#
|
1591
|
+
# because parameter name repetition is precisely what Rails seeks to distinguish
|
1592
|
+
# the elements of the array. For each item with a checked check box you
|
1593
|
+
# get an extra ghost item with only that attribute, assigned to "0".
|
1594
|
+
#
|
1595
|
+
# In that case it is preferable to either use +check_box_tag+ or to use
|
1596
|
+
# hashes instead of arrays.
|
1597
|
+
#
|
1598
|
+
# # Let's say that @post.validated? is 1:
|
1599
|
+
# check_box("post", "validated")
|
1600
|
+
# # => <input name="post[validated]" type="hidden" value="0" />
|
1601
|
+
# # <input checked="checked" type="checkbox" id="post_validated" name="post[validated]" value="1" />
|
1602
|
+
#
|
1603
|
+
# # Let's say that @puppy.gooddog is "no":
|
1604
|
+
# check_box("puppy", "gooddog", {}, "yes", "no")
|
1605
|
+
# # => <input name="puppy[gooddog]" type="hidden" value="no" />
|
1606
|
+
# # <input type="checkbox" id="puppy_gooddog" name="puppy[gooddog]" value="yes" />
|
1607
|
+
#
|
1608
|
+
# check_box("eula", "accepted", { class: 'eula_check' }, "yes", "no")
|
1609
|
+
# # => <input name="eula[accepted]" type="hidden" value="no" />
|
1610
|
+
# # <input type="checkbox" class="eula_check" id="eula_accepted" name="eula[accepted]" value="yes" />
|
1611
|
+
def check_box(method, options = {}, checked_value = "1", unchecked_value = "0")
|
1612
|
+
@template.check_box(@object_name, method, objectify_options(options), checked_value, unchecked_value)
|
1613
|
+
end
|
1614
|
+
|
1615
|
+
# Returns a radio button tag for accessing a specified attribute (identified by +method+) on an object
|
1616
|
+
# assigned to the template (identified by +object+). If the current value of +method+ is +tag_value+ the
|
1617
|
+
# radio button will be checked.
|
1618
|
+
#
|
1619
|
+
# To force the radio button to be checked pass <tt>checked: true</tt> in the
|
1620
|
+
# +options+ hash. You may pass HTML options there as well.
|
1621
|
+
#
|
1622
|
+
# # Let's say that @post.category returns "rails":
|
1623
|
+
# radio_button("post", "category", "rails")
|
1624
|
+
# radio_button("post", "category", "java")
|
1625
|
+
# # => <input type="radio" id="post_category_rails" name="post[category]" value="rails" checked="checked" />
|
1626
|
+
# # <input type="radio" id="post_category_java" name="post[category]" value="java" />
|
1627
|
+
#
|
1628
|
+
# radio_button("user", "receive_newsletter", "yes")
|
1629
|
+
# radio_button("user", "receive_newsletter", "no")
|
1630
|
+
# # => <input type="radio" id="user_receive_newsletter_yes" name="user[receive_newsletter]" value="yes" />
|
1631
|
+
# # <input type="radio" id="user_receive_newsletter_no" name="user[receive_newsletter]" value="no" checked="checked" />
|
1632
|
+
def radio_button(method, tag_value, options = {})
|
1633
|
+
@template.radio_button(@object_name, method, tag_value, objectify_options(options))
|
1634
|
+
end
|
1635
|
+
|
1636
|
+
# Returns a hidden input tag tailored for accessing a specified attribute (identified by +method+) on an object
|
1637
|
+
# assigned to the template (identified by +object+). Additional options on the input tag can be passed as a
|
1638
|
+
# hash with +options+. These options will be tagged onto the HTML as an HTML element attribute as in the example
|
1639
|
+
# shown.
|
1640
|
+
#
|
1641
|
+
# ==== Examples
|
1642
|
+
# hidden_field(:signup, :pass_confirm)
|
1643
|
+
# # => <input type="hidden" id="signup_pass_confirm" name="signup[pass_confirm]" value="#{@signup.pass_confirm}" />
|
1644
|
+
#
|
1645
|
+
# hidden_field(:post, :tag_list)
|
1646
|
+
# # => <input type="hidden" id="post_tag_list" name="post[tag_list]" value="#{@post.tag_list}" />
|
1647
|
+
#
|
1648
|
+
# hidden_field(:user, :token)
|
1649
|
+
# # => <input type="hidden" id="user_token" name="user[token]" value="#{@user.token}" />
|
1650
|
+
#
|
1331
1651
|
def hidden_field(method, options = {})
|
1332
1652
|
@emitted_hidden_id = true if method == :id
|
1333
1653
|
@template.hidden_field(@object_name, method, objectify_options(options))
|
1334
1654
|
end
|
1335
1655
|
|
1656
|
+
# Returns a file upload input tag tailored for accessing a specified attribute (identified by +method+) on an object
|
1657
|
+
# assigned to the template (identified by +object+). Additional options on the input tag can be passed as a
|
1658
|
+
# hash with +options+. These options will be tagged onto the HTML as an HTML element attribute as in the example
|
1659
|
+
# shown.
|
1660
|
+
#
|
1661
|
+
# Using this method inside a +form_for+ block will set the enclosing form's encoding to <tt>multipart/form-data</tt>.
|
1662
|
+
#
|
1663
|
+
# ==== Options
|
1664
|
+
# * Creates standard HTML attributes for the tag.
|
1665
|
+
# * <tt>:disabled</tt> - If set to true, the user will not be able to use this input.
|
1666
|
+
# * <tt>:multiple</tt> - If set to true, *in most updated browsers* the user will be allowed to select multiple files.
|
1667
|
+
# * <tt>:accept</tt> - If set to one or multiple mime-types, the user will be suggested a filter when choosing a file. You still need to set up model validations.
|
1668
|
+
#
|
1669
|
+
# ==== Examples
|
1670
|
+
# file_field(:user, :avatar)
|
1671
|
+
# # => <input type="file" id="user_avatar" name="user[avatar]" />
|
1672
|
+
#
|
1673
|
+
# file_field(:post, :image, :multiple => true)
|
1674
|
+
# # => <input type="file" id="post_image" name="post[image]" multiple="true" />
|
1675
|
+
#
|
1676
|
+
# file_field(:post, :attached, accept: 'text/html')
|
1677
|
+
# # => <input accept="text/html" type="file" id="post_attached" name="post[attached]" />
|
1678
|
+
#
|
1679
|
+
# file_field(:post, :image, accept: 'image/png,image/gif,image/jpeg')
|
1680
|
+
# # => <input type="file" id="post_image" name="post[image]" accept="image/png,image/gif,image/jpeg" />
|
1681
|
+
#
|
1682
|
+
# file_field(:attachment, :file, class: 'file_input')
|
1683
|
+
# # => <input type="file" id="attachment_file" name="attachment[file]" class="file_input" />
|
1336
1684
|
def file_field(method, options = {})
|
1337
1685
|
self.multipart = true
|
1338
1686
|
@template.file_field(@object_name, method, objectify_options(options))
|
@@ -1379,14 +1727,14 @@ module ActionView
|
|
1379
1727
|
# <% end %>
|
1380
1728
|
#
|
1381
1729
|
# In the example above, if @post is a new record, it will use "Create Post" as
|
1382
|
-
#
|
1730
|
+
# button label, otherwise, it uses "Update Post".
|
1383
1731
|
#
|
1384
|
-
# Those labels can be customized using I18n, under the helpers.submit key
|
1385
|
-
# the %{model} as translation interpolation:
|
1732
|
+
# Those labels can be customized using I18n, under the helpers.submit key
|
1733
|
+
# (the same as submit helper) and accept the %{model} as translation interpolation:
|
1386
1734
|
#
|
1387
1735
|
# en:
|
1388
1736
|
# helpers:
|
1389
|
-
#
|
1737
|
+
# submit:
|
1390
1738
|
# create: "Create a %{model}"
|
1391
1739
|
# update: "Confirm changes to %{model}"
|
1392
1740
|
#
|
@@ -1394,14 +1742,25 @@ module ActionView
|
|
1394
1742
|
#
|
1395
1743
|
# en:
|
1396
1744
|
# helpers:
|
1397
|
-
#
|
1745
|
+
# submit:
|
1398
1746
|
# post:
|
1399
1747
|
# create: "Add %{model}"
|
1400
1748
|
#
|
1401
|
-
|
1749
|
+
# ==== Examples
|
1750
|
+
# button("Create a post")
|
1751
|
+
# # => <button name='button' type='submit'>Create post</button>
|
1752
|
+
#
|
1753
|
+
# button do
|
1754
|
+
# content_tag(:strong, 'Ask me!')
|
1755
|
+
# end
|
1756
|
+
# # => <button name='button' type='submit'>
|
1757
|
+
# # <strong>Ask me!</strong>
|
1758
|
+
# # </button>
|
1759
|
+
#
|
1760
|
+
def button(value = nil, options = {}, &block)
|
1402
1761
|
value, options = nil, value if value.is_a?(Hash)
|
1403
1762
|
value ||= submit_default_value
|
1404
|
-
@template.button_tag(value, options)
|
1763
|
+
@template.button_tag(value, options, &block)
|
1405
1764
|
end
|
1406
1765
|
|
1407
1766
|
def emitted_hidden_id?
|
@@ -1410,7 +1769,7 @@ module ActionView
|
|
1410
1769
|
|
1411
1770
|
private
|
1412
1771
|
def objectify_options(options)
|
1413
|
-
@default_options.merge(options.merge(:
|
1772
|
+
@default_options.merge(options.merge(object: @object))
|
1414
1773
|
end
|
1415
1774
|
|
1416
1775
|
def submit_default_value
|
@@ -1428,7 +1787,7 @@ module ActionView
|
|
1428
1787
|
defaults << :"helpers.submit.#{key}"
|
1429
1788
|
defaults << "#{key.to_s.humanize} #{model}"
|
1430
1789
|
|
1431
|
-
I18n.t(defaults.shift, :
|
1790
|
+
I18n.t(defaults.shift, model: model, default: defaults)
|
1432
1791
|
end
|
1433
1792
|
|
1434
1793
|
def nested_attributes_association?(association_name)
|
@@ -1440,7 +1799,7 @@ module ActionView
|
|
1440
1799
|
association = convert_to_model(association)
|
1441
1800
|
|
1442
1801
|
if association.respond_to?(:persisted?)
|
1443
|
-
association = [association] if @object.send(association_name).
|
1802
|
+
association = [association] if @object.send(association_name).respond_to?(:to_ary)
|
1444
1803
|
elsif !association.respond_to?(:to_ary)
|
1445
1804
|
association = @object.send(association_name)
|
1446
1805
|
end
|
@@ -1449,7 +1808,8 @@ module ActionView
|
|
1449
1808
|
explicit_child_index = options[:child_index]
|
1450
1809
|
output = ActiveSupport::SafeBuffer.new
|
1451
1810
|
association.each do |child|
|
1452
|
-
|
1811
|
+
options[:child_index] = nested_child_index(name) unless explicit_child_index
|
1812
|
+
output << fields_for_nested_model("#{name}[#{options[:child_index]}]", child, options, block)
|
1453
1813
|
end
|
1454
1814
|
output
|
1455
1815
|
elsif association
|
@@ -1457,30 +1817,27 @@ module ActionView
|
|
1457
1817
|
end
|
1458
1818
|
end
|
1459
1819
|
|
1460
|
-
def fields_for_nested_model(name, object,
|
1820
|
+
def fields_for_nested_model(name, object, fields_options, block)
|
1461
1821
|
object = convert_to_model(object)
|
1822
|
+
emit_hidden_id = object.persisted? && fields_options.fetch(:include_id) {
|
1823
|
+
options.fetch(:include_id, true)
|
1824
|
+
}
|
1462
1825
|
|
1463
|
-
|
1464
|
-
|
1465
|
-
|
1466
|
-
|
1826
|
+
@template.fields_for(name, object, fields_options) do |f|
|
1827
|
+
output = @template.capture(f, &block)
|
1828
|
+
output.concat f.hidden_field(:id) if output && emit_hidden_id && !f.emitted_hidden_id?
|
1829
|
+
output
|
1830
|
+
end
|
1467
1831
|
end
|
1468
1832
|
|
1469
1833
|
def nested_child_index(name)
|
1470
1834
|
@nested_child_index[name] ||= -1
|
1471
1835
|
@nested_child_index[name] += 1
|
1472
1836
|
end
|
1473
|
-
|
1474
|
-
def convert_to_model(object)
|
1475
|
-
object.respond_to?(:to_model) ? object.to_model : object
|
1476
|
-
end
|
1477
1837
|
end
|
1478
1838
|
end
|
1479
1839
|
|
1480
1840
|
ActiveSupport.on_load(:action_view) do
|
1481
|
-
|
1482
|
-
cattr_accessor :default_form_builder
|
1483
|
-
@@default_form_builder = ::ActionView::Helpers::FormBuilder
|
1484
|
-
end
|
1841
|
+
cattr_accessor(:default_form_builder) { ::ActionView::Helpers::FormBuilder }
|
1485
1842
|
end
|
1486
1843
|
end
|