decentworks-date-support 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gem_rbs_collection/actionpack/7.2/.rbs_meta.yaml +9 -0
- data/.gem_rbs_collection/actionpack/7.2/actioncontroller-7.2.rbs +14 -0
- data/.gem_rbs_collection/actionpack/7.2/actioncontroller.rbs +290 -0
- data/.gem_rbs_collection/actionpack/7.2/actiondispatch.rbs +349 -0
- data/.gem_rbs_collection/actionpack/7.2/actionpack-generated.rbs +11497 -0
- data/.gem_rbs_collection/actionpack/7.2/patch.rbs +77 -0
- data/.gem_rbs_collection/actionview/6.0/.rbs_meta.yaml +9 -0
- data/.gem_rbs_collection/actionview/6.0/actionview-generated.rbs +8976 -0
- data/.gem_rbs_collection/actionview/6.0/actionview.rbs +1940 -0
- data/.gem_rbs_collection/actionview/6.0/patch.rbs +57 -0
- data/.gem_rbs_collection/activesupport/7.0/.rbs_meta.yaml +9 -0
- data/.gem_rbs_collection/activesupport/7.0/activesupport-7.0.rbs +381 -0
- data/.gem_rbs_collection/activesupport/7.0/activesupport-generated.rbs +11633 -0
- data/.gem_rbs_collection/activesupport/7.0/activesupport.rbs +724 -0
- data/.gem_rbs_collection/activesupport/7.0/manifest.yaml +15 -0
- data/.gem_rbs_collection/activesupport/7.0/patch.rbs +55 -0
- data/.gem_rbs_collection/ast/2.4/.rbs_meta.yaml +9 -0
- data/.gem_rbs_collection/ast/2.4/ast.rbs +73 -0
- data/.gem_rbs_collection/cgi/0.5/.rbs_meta.yaml +9 -0
- data/.gem_rbs_collection/cgi/0.5/core.rbs +925 -0
- data/.gem_rbs_collection/cgi/0.5/manifest.yaml +3 -0
- data/.gem_rbs_collection/concurrent-ruby/1.1/.rbs_meta.yaml +9 -0
- data/.gem_rbs_collection/concurrent-ruby/1.1/array.rbs +4 -0
- data/.gem_rbs_collection/concurrent-ruby/1.1/atomic_reference.rbs +16 -0
- data/.gem_rbs_collection/concurrent-ruby/1.1/executor.rbs +96 -0
- data/.gem_rbs_collection/concurrent-ruby/1.1/hash.rbs +4 -0
- data/.gem_rbs_collection/concurrent-ruby/1.1/map.rbs +68 -0
- data/.gem_rbs_collection/concurrent-ruby/1.1/promises.rbs +249 -0
- data/.gem_rbs_collection/concurrent-ruby/1.1/set.rbs +4 -0
- data/.gem_rbs_collection/concurrent-ruby/1.1/timer_task.rbs +47 -0
- data/.gem_rbs_collection/concurrent-ruby/1.1/utility/processor_counter.rbs +5 -0
- data/.gem_rbs_collection/connection_pool/2.4/.rbs_meta.yaml +9 -0
- data/.gem_rbs_collection/connection_pool/2.4/connection_pool.rbs +34 -0
- data/.gem_rbs_collection/connection_pool/2.4/manifest.yaml +2 -0
- data/.gem_rbs_collection/diff-lcs/1.5/.rbs_meta.yaml +9 -0
- data/.gem_rbs_collection/diff-lcs/1.5/diff-lcs.rbs +11 -0
- data/.gem_rbs_collection/faker/3.4.2/.rbs_meta.yaml +9 -0
- data/.gem_rbs_collection/faker/3.4.2/faker-2.23.rbs +90 -0
- data/.gem_rbs_collection/faker/3.4.2/faker.rbs +18 -0
- data/.gem_rbs_collection/i18n/1.10/.rbs_meta.yaml +9 -0
- data/.gem_rbs_collection/i18n/1.10/backend.rbs +274 -0
- data/.gem_rbs_collection/i18n/1.10/i18n.rbs +116 -0
- data/.gem_rbs_collection/lint_roller/1.1/.rbs_meta.yaml +9 -0
- data/.gem_rbs_collection/lint_roller/1.1/lint_roller.rbs +48 -0
- data/.gem_rbs_collection/minitest/5.25/.rbs_meta.yaml +9 -0
- data/.gem_rbs_collection/minitest/5.25/minitest/abstract_reporter.rbs +52 -0
- data/.gem_rbs_collection/minitest/5.25/minitest/assertion.rbs +17 -0
- data/.gem_rbs_collection/minitest/5.25/minitest/assertions.rbs +590 -0
- data/.gem_rbs_collection/minitest/5.25/minitest/backtrace_filter.rbs +23 -0
- data/.gem_rbs_collection/minitest/5.25/minitest/bench_spec.rbs +102 -0
- data/.gem_rbs_collection/minitest/5.25/minitest/benchmark.rbs +259 -0
- data/.gem_rbs_collection/minitest/5.25/minitest/composite_reporter.rbs +25 -0
- data/.gem_rbs_collection/minitest/5.25/minitest/compress.rbs +13 -0
- data/.gem_rbs_collection/minitest/5.25/minitest/error_on_warning.rbs +3 -0
- data/.gem_rbs_collection/minitest/5.25/minitest/expectation.rbs +2 -0
- data/.gem_rbs_collection/minitest/5.25/minitest/expectations.rbs +21 -0
- data/.gem_rbs_collection/minitest/5.25/minitest/guard.rbs +64 -0
- data/.gem_rbs_collection/minitest/5.25/minitest/mock.rbs +64 -0
- data/.gem_rbs_collection/minitest/5.25/minitest/parallel/executor.rbs +46 -0
- data/.gem_rbs_collection/minitest/5.25/minitest/parallel/test/class_methods.rbs +5 -0
- data/.gem_rbs_collection/minitest/5.25/minitest/parallel/test.rbs +3 -0
- data/.gem_rbs_collection/minitest/5.25/minitest/parallel.rbs +2 -0
- data/.gem_rbs_collection/minitest/5.25/minitest/pride_io.rbs +62 -0
- data/.gem_rbs_collection/minitest/5.25/minitest/pride_lol.rbs +19 -0
- data/.gem_rbs_collection/minitest/5.25/minitest/progress_reporter.rbs +11 -0
- data/.gem_rbs_collection/minitest/5.25/minitest/reportable.rbs +53 -0
- data/.gem_rbs_collection/minitest/5.25/minitest/reporter.rbs +5 -0
- data/.gem_rbs_collection/minitest/5.25/minitest/result.rbs +28 -0
- data/.gem_rbs_collection/minitest/5.25/minitest/runnable.rbs +163 -0
- data/.gem_rbs_collection/minitest/5.25/minitest/skip.rbs +6 -0
- data/.gem_rbs_collection/minitest/5.25/minitest/spec/dsl/instance_methods.rbs +48 -0
- data/.gem_rbs_collection/minitest/5.25/minitest/spec/dsl.rbs +129 -0
- data/.gem_rbs_collection/minitest/5.25/minitest/spec.rbs +11 -0
- data/.gem_rbs_collection/minitest/5.25/minitest/statistics_reporter.rbs +81 -0
- data/.gem_rbs_collection/minitest/5.25/minitest/summary_reporter.rbs +18 -0
- data/.gem_rbs_collection/minitest/5.25/minitest/test/lifecycle_hooks.rbs +92 -0
- data/.gem_rbs_collection/minitest/5.25/minitest/test.rbs +69 -0
- data/.gem_rbs_collection/minitest/5.25/minitest/unexpected_error.rbs +12 -0
- data/.gem_rbs_collection/minitest/5.25/minitest/unexpected_warning.rbs +6 -0
- data/.gem_rbs_collection/minitest/5.25/minitest/unit/test_case.rbs +3 -0
- data/.gem_rbs_collection/minitest/5.25/minitest/unit.rbs +4 -0
- data/.gem_rbs_collection/minitest/5.25/minitest.rbs +115 -0
- data/.gem_rbs_collection/nokogiri/1.11/.rbs_meta.yaml +9 -0
- data/.gem_rbs_collection/nokogiri/1.11/nokogiri.rbs +2332 -0
- data/.gem_rbs_collection/nokogiri/1.11/patch.rbs +4 -0
- data/.gem_rbs_collection/parallel/1.20/.rbs_meta.yaml +9 -0
- data/.gem_rbs_collection/parallel/1.20/parallel.rbs +86 -0
- data/.gem_rbs_collection/parser/3.2/.rbs_meta.yaml +9 -0
- data/.gem_rbs_collection/parser/3.2/manifest.yaml +7 -0
- data/.gem_rbs_collection/parser/3.2/parser.rbs +194 -0
- data/.gem_rbs_collection/parser/3.2/polyfill.rbs +4 -0
- data/.gem_rbs_collection/rack/2.2/.rbs_meta.yaml +9 -0
- data/.gem_rbs_collection/rack/2.2/manifest.yaml +3 -0
- data/.gem_rbs_collection/rack/2.2/rack.rbs +1480 -0
- data/.gem_rbs_collection/rails-dom-testing/2.0/.rbs_meta.yaml +9 -0
- data/.gem_rbs_collection/rails-dom-testing/2.0/rails-dom-testing.rbs +51 -0
- data/.gem_rbs_collection/rails-html-sanitizer/1.6/.rbs_meta.yaml +9 -0
- data/.gem_rbs_collection/rails-html-sanitizer/1.6/rails-html-sanitizer.rbs +53 -0
- data/.gem_rbs_collection/railties/6.0/.rbs_meta.yaml +9 -0
- data/.gem_rbs_collection/railties/6.0/manifest.yaml +2 -0
- data/.gem_rbs_collection/railties/6.0/patch.rbs +17 -0
- data/.gem_rbs_collection/railties/6.0/railties-generated.rbs +4822 -0
- data/.gem_rbs_collection/railties/6.0/railties.rbs +44 -0
- data/.gem_rbs_collection/rainbow/3.0/.rbs_meta.yaml +9 -0
- data/.gem_rbs_collection/rainbow/3.0/global.rbs +7 -0
- data/.gem_rbs_collection/rainbow/3.0/presenter.rbs +209 -0
- data/.gem_rbs_collection/rainbow/3.0/rainbow.rbs +5 -0
- data/.gem_rbs_collection/rake/13.0/.rbs_meta.yaml +9 -0
- data/.gem_rbs_collection/rake/13.0/manifest.yaml +2 -0
- data/.gem_rbs_collection/rake/13.0/rake.rbs +39 -0
- data/.gem_rbs_collection/regexp_parser/2.8/.rbs_meta.yaml +9 -0
- data/.gem_rbs_collection/regexp_parser/2.8/regexp_parser.rbs +17 -0
- data/.gem_rbs_collection/rubocop/1.57/.rbs_meta.yaml +9 -0
- data/.gem_rbs_collection/rubocop/1.57/rubocop.rbs +208 -0
- data/.gem_rbs_collection/rubocop-ast/1.46/.rbs_meta.yaml +9 -0
- data/.gem_rbs_collection/rubocop-ast/1.46/rubocop-ast.rbs +903 -0
- data/.gem_rbs_collection/thor/1.2/.rbs_meta.yaml +9 -0
- data/.gem_rbs_collection/thor/1.2/thor.rbs +62 -0
- data/.gem_rbs_collection/tzinfo/2.0/.rbs_meta.yaml +9 -0
- data/.gem_rbs_collection/tzinfo/2.0/manifest.yaml +7 -0
- data/.gem_rbs_collection/tzinfo/2.0/tzinfo.rbs +600 -0
- data/.rspec +3 -0
- data/.rubocop.yml +8 -0
- data/.ruby-version +1 -0
- data/CHANGELOG.md +5 -0
- data/CODE_OF_CONDUCT.md +132 -0
- data/LICENSE +21 -0
- data/LICENSE.txt +21 -0
- data/README.md +54 -0
- data/Rakefile +12 -0
- data/Steepfile +43 -0
- data/lib/decentworks/active_support/time_with_zone_support.rb +327 -0
- data/lib/decentworks/date/support/version.rb +9 -0
- data/lib/decentworks/date_support.rb +323 -0
- data/lib/decentworks_date_support.rb +4 -0
- data/rbs_collection.lock.yaml +288 -0
- data/rbs_collection.yaml +41 -0
- data/sig/decentworks/active_support/time_with_zone_support.rbs +109 -0
- data/sig/decentworks/date/support.rbs +8 -0
- data/sig/decentworks/date_support.rbs +107 -0
- metadata +197 -0
|
@@ -0,0 +1,1940 @@
|
|
|
1
|
+
module ActionView
|
|
2
|
+
module Helpers
|
|
3
|
+
module CaptureHelper
|
|
4
|
+
# Calling <tt>content_for</tt> stores a block of markup in an identifier for later use.
|
|
5
|
+
# In order to access this stored content in other templates, helper modules
|
|
6
|
+
# or the layout, you would pass the identifier as an argument to <tt>content_for</tt>.
|
|
7
|
+
#
|
|
8
|
+
# Note: <tt>yield</tt> can still be used to retrieve the stored content, but calling
|
|
9
|
+
# <tt>yield</tt> doesn't work in helper modules, while <tt>content_for</tt> does.
|
|
10
|
+
#
|
|
11
|
+
# <% content_for :not_authorized do %>
|
|
12
|
+
# alert('You are not authorized to do that!')
|
|
13
|
+
# <% end %>
|
|
14
|
+
#
|
|
15
|
+
# You can then use <tt>content_for :not_authorized</tt> anywhere in your templates.
|
|
16
|
+
#
|
|
17
|
+
# <%= content_for :not_authorized if current_user.nil? %>
|
|
18
|
+
#
|
|
19
|
+
# This is equivalent to:
|
|
20
|
+
#
|
|
21
|
+
# <%= yield :not_authorized if current_user.nil? %>
|
|
22
|
+
#
|
|
23
|
+
# <tt>content_for</tt>, however, can also be used in helper modules.
|
|
24
|
+
#
|
|
25
|
+
# module StorageHelper
|
|
26
|
+
# def stored_content
|
|
27
|
+
# content_for(:storage) || "Your storage is empty"
|
|
28
|
+
# end
|
|
29
|
+
# end
|
|
30
|
+
#
|
|
31
|
+
# This helper works just like normal helpers.
|
|
32
|
+
#
|
|
33
|
+
# <%= stored_content %>
|
|
34
|
+
#
|
|
35
|
+
# You can also use the <tt>yield</tt> syntax alongside an existing call to
|
|
36
|
+
# <tt>yield</tt> in a layout. For example:
|
|
37
|
+
#
|
|
38
|
+
# <%# This is the layout %>
|
|
39
|
+
# <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
|
|
40
|
+
# <head>
|
|
41
|
+
# <title>My Website</title>
|
|
42
|
+
# <%= yield :script %>
|
|
43
|
+
# </head>
|
|
44
|
+
# <body>
|
|
45
|
+
# <%= yield %>
|
|
46
|
+
# </body>
|
|
47
|
+
# </html>
|
|
48
|
+
#
|
|
49
|
+
# And now, we'll create a view that has a <tt>content_for</tt> call that
|
|
50
|
+
# creates the <tt>script</tt> identifier.
|
|
51
|
+
#
|
|
52
|
+
# <%# This is our view %>
|
|
53
|
+
# Please login!
|
|
54
|
+
#
|
|
55
|
+
# <% content_for :script do %>
|
|
56
|
+
# <script>alert('You are not authorized to view this page!')</script>
|
|
57
|
+
# <% end %>
|
|
58
|
+
#
|
|
59
|
+
# Then, in another view, you could to do something like this:
|
|
60
|
+
#
|
|
61
|
+
# <%= link_to 'Logout', action: 'logout', remote: true %>
|
|
62
|
+
#
|
|
63
|
+
# <% content_for :script do %>
|
|
64
|
+
# <%= javascript_include_tag :defaults %>
|
|
65
|
+
# <% end %>
|
|
66
|
+
#
|
|
67
|
+
# That will place +script+ tags for your default set of JavaScript files on the page;
|
|
68
|
+
# this technique is useful if you'll only be using these scripts in a few views.
|
|
69
|
+
#
|
|
70
|
+
# Note that <tt>content_for</tt> concatenates (default) the blocks it is given for a particular
|
|
71
|
+
# identifier in order. For example:
|
|
72
|
+
#
|
|
73
|
+
# <% content_for :navigation do %>
|
|
74
|
+
# <li><%= link_to 'Home', action: 'index' %></li>
|
|
75
|
+
# <% end %>
|
|
76
|
+
#
|
|
77
|
+
# And in another place:
|
|
78
|
+
#
|
|
79
|
+
# <% content_for :navigation do %>
|
|
80
|
+
# <li><%= link_to 'Login', action: 'login' %></li>
|
|
81
|
+
# <% end %>
|
|
82
|
+
#
|
|
83
|
+
# Then, in another template or layout, this code would render both links in order:
|
|
84
|
+
#
|
|
85
|
+
# <ul><%= content_for :navigation %></ul>
|
|
86
|
+
#
|
|
87
|
+
# If the flush parameter is +true+ <tt>content_for</tt> replaces the blocks it is given. For example:
|
|
88
|
+
#
|
|
89
|
+
# <% content_for :navigation do %>
|
|
90
|
+
# <li><%= link_to 'Home', action: 'index' %></li>
|
|
91
|
+
# <% end %>
|
|
92
|
+
#
|
|
93
|
+
# <%# Add some other content, or use a different template: %>
|
|
94
|
+
#
|
|
95
|
+
# <% content_for :navigation, flush: true do %>
|
|
96
|
+
# <li><%= link_to 'Login', action: 'login' %></li>
|
|
97
|
+
# <% end %>
|
|
98
|
+
#
|
|
99
|
+
# Then, in another template or layout, this code would render only the last link:
|
|
100
|
+
#
|
|
101
|
+
# <ul><%= content_for :navigation %></ul>
|
|
102
|
+
#
|
|
103
|
+
# Lastly, simple content can be passed as a parameter:
|
|
104
|
+
#
|
|
105
|
+
# <% content_for :script, javascript_include_tag(:defaults) %>
|
|
106
|
+
#
|
|
107
|
+
# WARNING: <tt>content_for</tt> is ignored in caches. So you shouldn't use it for elements that will be fragment cached.
|
|
108
|
+
def content_for: (untyped name, ?untyped? content, ?::Hash[untyped, untyped] options) ?{ () -> untyped } -> void
|
|
109
|
+
end
|
|
110
|
+
end
|
|
111
|
+
end
|
|
112
|
+
|
|
113
|
+
module ActionView
|
|
114
|
+
module Helpers
|
|
115
|
+
class FormBuilder
|
|
116
|
+
def text_field: (untyped method, ?Hash[untyped, untyped] options) -> String
|
|
117
|
+
def password_field: (untyped method, ?Hash[untyped, untyped] options) -> String
|
|
118
|
+
def text_area: (untyped method, ?Hash[untyped, untyped] options) -> String
|
|
119
|
+
def color_field: (untyped method, ?Hash[untyped, untyped] options) -> String
|
|
120
|
+
def search_field: (untyped method, ?Hash[untyped, untyped] options) -> String
|
|
121
|
+
def telephone_field: (untyped method, ?Hash[untyped, untyped] options) -> String
|
|
122
|
+
def phone_field: (untyped method, ?Hash[untyped, untyped] options) -> String
|
|
123
|
+
def date_field: (untyped method, ?Hash[untyped, untyped] options) -> String
|
|
124
|
+
def time_field: (untyped method, ?Hash[untyped, untyped] options) -> String
|
|
125
|
+
def datetime_field: (untyped method, ?Hash[untyped, untyped] options) -> String
|
|
126
|
+
def datetime_local_field: (untyped method, ?Hash[untyped, untyped] options) -> String
|
|
127
|
+
def month_field: (untyped method, ?Hash[untyped, untyped] options) -> String
|
|
128
|
+
def week_field: (untyped method, ?Hash[untyped, untyped] options) -> String
|
|
129
|
+
def url_field: (untyped method, ?Hash[untyped, untyped] options) -> String
|
|
130
|
+
def email_field: (untyped method, ?Hash[untyped, untyped] options) -> String
|
|
131
|
+
def number_field: (untyped method, ?Hash[untyped, untyped] options) -> String
|
|
132
|
+
def range_field: (untyped method, ?Hash[untyped, untyped] options) -> String
|
|
133
|
+
|
|
134
|
+
# Add the submit button for the given form. When no value is given, it checks
|
|
135
|
+
# if the object is a new resource or not to create the proper label:
|
|
136
|
+
#
|
|
137
|
+
# <%= form_for @post do |f| %>
|
|
138
|
+
# <%= f.button %>
|
|
139
|
+
# <% end %>
|
|
140
|
+
#
|
|
141
|
+
# In the example above, if <tt>@post</tt> is a new record, it will use "Create Post" as
|
|
142
|
+
# button label; otherwise, it uses "Update Post".
|
|
143
|
+
#
|
|
144
|
+
# Those labels can be customized using I18n under the +helpers.submit+ key
|
|
145
|
+
# (the same as submit helper) and using <tt>%{model}</tt> for translation interpolation:
|
|
146
|
+
#
|
|
147
|
+
# en:
|
|
148
|
+
# helpers:
|
|
149
|
+
# submit:
|
|
150
|
+
# create: "Create a %{model}"
|
|
151
|
+
# update: "Confirm changes to %{model}"
|
|
152
|
+
#
|
|
153
|
+
# It also searches for a key specific to the given object:
|
|
154
|
+
#
|
|
155
|
+
# en:
|
|
156
|
+
# helpers:
|
|
157
|
+
# submit:
|
|
158
|
+
# post:
|
|
159
|
+
# create: "Add %{model}"
|
|
160
|
+
#
|
|
161
|
+
# ==== Examples
|
|
162
|
+
# button("Create post")
|
|
163
|
+
# # => <button name='button' type='submit'>Create post</button>
|
|
164
|
+
#
|
|
165
|
+
# button do
|
|
166
|
+
# content_tag(:strong, 'Ask me!')
|
|
167
|
+
# end
|
|
168
|
+
# # => <button name='button' type='submit'>
|
|
169
|
+
# # <strong>Ask me!</strong>
|
|
170
|
+
# # </button>
|
|
171
|
+
#
|
|
172
|
+
def button: (?untyped? value, ?::Hash[untyped, untyped] options) ?{ () -> untyped } -> untyped
|
|
173
|
+
|
|
174
|
+
# Returns a label tag tailored for labelling an input field for a specified attribute (identified by +method+) on an object
|
|
175
|
+
# assigned to the template (identified by +object+). The text of label will default to the attribute name unless a translation
|
|
176
|
+
# is found in the current I18n locale (through helpers.label.<modelname>.<attribute>) or you specify it explicitly.
|
|
177
|
+
# Additional options on the label tag can be passed as a hash with +options+. These options will be tagged
|
|
178
|
+
# onto the HTML as an HTML element attribute as in the example shown, except for the <tt>:value</tt> option, which is designed to
|
|
179
|
+
# target labels for radio_button tags (where the value is used in the ID of the input tag).
|
|
180
|
+
#
|
|
181
|
+
# ==== Examples
|
|
182
|
+
# label(:title)
|
|
183
|
+
# # => <label for="post_title">Title</label>
|
|
184
|
+
#
|
|
185
|
+
# You can localize your labels based on model and attribute names.
|
|
186
|
+
# For example you can define the following in your locale (e.g. en.yml)
|
|
187
|
+
#
|
|
188
|
+
# helpers:
|
|
189
|
+
# label:
|
|
190
|
+
# post:
|
|
191
|
+
# body: "Write your entire text here"
|
|
192
|
+
#
|
|
193
|
+
# Which then will result in
|
|
194
|
+
#
|
|
195
|
+
# label(:body)
|
|
196
|
+
# # => <label for="post_body">Write your entire text here</label>
|
|
197
|
+
#
|
|
198
|
+
# Localization can also be based purely on the translation of the attribute-name
|
|
199
|
+
# (if you are using ActiveRecord):
|
|
200
|
+
#
|
|
201
|
+
# activerecord:
|
|
202
|
+
# attributes:
|
|
203
|
+
# post:
|
|
204
|
+
# cost: "Total cost"
|
|
205
|
+
#
|
|
206
|
+
# label(:cost)
|
|
207
|
+
# # => <label for="post_cost">Total cost</label>
|
|
208
|
+
#
|
|
209
|
+
# label(:title, "A short title")
|
|
210
|
+
# # => <label for="post_title">A short title</label>
|
|
211
|
+
#
|
|
212
|
+
# label(:title, "A short title", class: "title_label")
|
|
213
|
+
# # => <label for="post_title" class="title_label">A short title</label>
|
|
214
|
+
#
|
|
215
|
+
# label(:privacy, "Public Post", value: "public")
|
|
216
|
+
# # => <label for="post_privacy_public">Public Post</label>
|
|
217
|
+
#
|
|
218
|
+
# label(:terms) do
|
|
219
|
+
# raw('Accept <a href="/terms">Terms</a>.')
|
|
220
|
+
# end
|
|
221
|
+
# # => <label for="post_terms">Accept <a href="/terms">Terms</a>.</label>
|
|
222
|
+
def label: (untyped method, ?untyped? text, ?::Hash[untyped, untyped] options) ?{ () -> untyped } -> untyped
|
|
223
|
+
|
|
224
|
+
# Wraps ActionView::Helpers::FormOptionsHelper#select for form builders:
|
|
225
|
+
#
|
|
226
|
+
# <%= form_for @post do |f| %>
|
|
227
|
+
# <%= f.select :person_id, Person.all.collect { |p| [ p.name, p.id ] }, include_blank: true %>
|
|
228
|
+
# <%= f.submit %>
|
|
229
|
+
# <% end %>
|
|
230
|
+
#
|
|
231
|
+
# Please refer to the documentation of the base helper for details.
|
|
232
|
+
def select: (untyped method, ?untyped? choices, ?::Hash[untyped, untyped] options, ?::Hash[untyped, untyped] html_options) ?{ () -> untyped } -> untyped
|
|
233
|
+
|
|
234
|
+
# Wraps ActionView::Helpers::FormOptionsHelper#collection_check_boxes for form builders:
|
|
235
|
+
#
|
|
236
|
+
# <%= form_for @post do |f| %>
|
|
237
|
+
# <%= f.collection_check_boxes :author_ids, Author.all, :id, :name_with_initial %>
|
|
238
|
+
# <%= f.submit %>
|
|
239
|
+
# <% end %>
|
|
240
|
+
#
|
|
241
|
+
# Please refer to the documentation of the base helper for details.
|
|
242
|
+
def collection_check_boxes: (untyped method, untyped collection, untyped value_method, untyped text_method, ?::Hash[untyped, untyped] options, ?::Hash[untyped, untyped] html_options) ?{ (ActionView::Helpers::Tags::CollectionCheckBoxes::CheckBoxBuilder) -> String } -> String
|
|
243
|
+
|
|
244
|
+
# Wraps ActionView::Helpers::FormOptionsHelper#collection_radio_buttons for form builders:
|
|
245
|
+
#
|
|
246
|
+
# <%= form_for @post do |f| %>
|
|
247
|
+
# <%= f.collection_radio_buttons :author_id, Author.all, :id, :name_with_initial %>
|
|
248
|
+
# <%= f.submit %>
|
|
249
|
+
# <% end %>
|
|
250
|
+
#
|
|
251
|
+
# Please refer to the documentation of the base helper for details.
|
|
252
|
+
def collection_radio_buttons: (untyped method, untyped collection, untyped value_method, untyped text_method, ?::Hash[untyped, untyped] options, ?::Hash[untyped, untyped] html_options) ?{ (ActionView::Helpers::Tags::CollectionRadioButtons::RadioButtonBuilder) -> String } -> String
|
|
253
|
+
end
|
|
254
|
+
end
|
|
255
|
+
end
|
|
256
|
+
|
|
257
|
+
module ActionView
|
|
258
|
+
module Helpers
|
|
259
|
+
# = Action View Form Option Helpers
|
|
260
|
+
# nodoc:
|
|
261
|
+
# Provides a number of methods for turning different kinds of containers into a set of option tags.
|
|
262
|
+
#
|
|
263
|
+
# The <tt>collection_select</tt>, <tt>select</tt> and <tt>time_zone_select</tt> methods take an <tt>options</tt> parameter, a hash:
|
|
264
|
+
#
|
|
265
|
+
# * <tt>:include_blank</tt> - set to true or a prompt string if the first option element of the select element is a blank. Useful if there is not a default value required for the select element.
|
|
266
|
+
#
|
|
267
|
+
# select("post", "category", Post::CATEGORIES, { include_blank: true })
|
|
268
|
+
#
|
|
269
|
+
# could become:
|
|
270
|
+
#
|
|
271
|
+
# <select name="post[category]" id="post_category">
|
|
272
|
+
# <option value=""></option>
|
|
273
|
+
# <option value="joke">joke</option>
|
|
274
|
+
# <option value="poem">poem</option>
|
|
275
|
+
# </select>
|
|
276
|
+
#
|
|
277
|
+
# Another common case is a select tag for a <tt>belongs_to</tt>-associated object.
|
|
278
|
+
#
|
|
279
|
+
# Example with <tt>@post.person_id => 2</tt>:
|
|
280
|
+
#
|
|
281
|
+
# select("post", "person_id", Person.all.collect { |p| [ p.name, p.id ] }, { include_blank: 'None' })
|
|
282
|
+
#
|
|
283
|
+
# could become:
|
|
284
|
+
#
|
|
285
|
+
# <select name="post[person_id]" id="post_person_id">
|
|
286
|
+
# <option value="">None</option>
|
|
287
|
+
# <option value="1">David</option>
|
|
288
|
+
# <option value="2" selected="selected">Eileen</option>
|
|
289
|
+
# <option value="3">Rafael</option>
|
|
290
|
+
# </select>
|
|
291
|
+
#
|
|
292
|
+
# * <tt>:prompt</tt> - set to true or a prompt string. When the select element doesn't have a value yet, this prepends an option with a generic prompt -- "Please select" -- or the given prompt string.
|
|
293
|
+
#
|
|
294
|
+
# select("post", "person_id", Person.all.collect { |p| [ p.name, p.id ] }, { prompt: 'Select Person' })
|
|
295
|
+
#
|
|
296
|
+
# could become:
|
|
297
|
+
#
|
|
298
|
+
# <select name="post[person_id]" id="post_person_id">
|
|
299
|
+
# <option value="">Select Person</option>
|
|
300
|
+
# <option value="1">David</option>
|
|
301
|
+
# <option value="2">Eileen</option>
|
|
302
|
+
# <option value="3">Rafael</option>
|
|
303
|
+
# </select>
|
|
304
|
+
#
|
|
305
|
+
# * <tt>:index</tt> - like the other form helpers, +select+ can accept an <tt>:index</tt> option to manually set the ID used in the resulting output. Unlike other helpers, +select+ expects this
|
|
306
|
+
# option to be in the +html_options+ parameter.
|
|
307
|
+
#
|
|
308
|
+
# select("album[]", "genre", %w[rap rock country], {}, { index: nil })
|
|
309
|
+
#
|
|
310
|
+
# becomes:
|
|
311
|
+
#
|
|
312
|
+
# <select name="album[][genre]" id="album__genre">
|
|
313
|
+
# <option value="rap">rap</option>
|
|
314
|
+
# <option value="rock">rock</option>
|
|
315
|
+
# <option value="country">country</option>
|
|
316
|
+
# </select>
|
|
317
|
+
#
|
|
318
|
+
# * <tt>:disabled</tt> - can be a single value or an array of values that will be disabled options in the final output.
|
|
319
|
+
#
|
|
320
|
+
# select("post", "category", Post::CATEGORIES, { disabled: 'restricted' })
|
|
321
|
+
#
|
|
322
|
+
# could become:
|
|
323
|
+
#
|
|
324
|
+
# <select name="post[category]" id="post_category">
|
|
325
|
+
# <option value=""></option>
|
|
326
|
+
# <option value="joke">joke</option>
|
|
327
|
+
# <option value="poem">poem</option>
|
|
328
|
+
# <option disabled="disabled" value="restricted">restricted</option>
|
|
329
|
+
# </select>
|
|
330
|
+
#
|
|
331
|
+
# When used with the <tt>collection_select</tt> helper, <tt>:disabled</tt> can also be a Proc that identifies those options that should be disabled.
|
|
332
|
+
#
|
|
333
|
+
# collection_select(:post, :category_id, Category.all, :id, :name, { disabled: -> (category) { category.archived? } })
|
|
334
|
+
#
|
|
335
|
+
# If the categories "2008 stuff" and "Christmas" return true when the method <tt>archived?</tt> is called, this would return:
|
|
336
|
+
# <select name="post[category_id]" id="post_category_id">
|
|
337
|
+
# <option value="1" disabled="disabled">2008 stuff</option>
|
|
338
|
+
# <option value="2" disabled="disabled">Christmas</option>
|
|
339
|
+
# <option value="3">Jokes</option>
|
|
340
|
+
# <option value="4">Poems</option>
|
|
341
|
+
# </select>
|
|
342
|
+
#
|
|
343
|
+
module FormOptionsHelper
|
|
344
|
+
# ERB::Util can mask some helpers like textilize. Make sure to include them.
|
|
345
|
+
include TextHelper
|
|
346
|
+
|
|
347
|
+
# Create a select tag and a series of contained option tags for the provided object and method.
|
|
348
|
+
# The option currently held by the object will be selected, provided that the object is available.
|
|
349
|
+
#
|
|
350
|
+
# There are two possible formats for the +choices+ parameter, corresponding to other helpers' output:
|
|
351
|
+
#
|
|
352
|
+
# * A flat collection (see +options_for_select+).
|
|
353
|
+
#
|
|
354
|
+
# * A nested collection (see +grouped_options_for_select+).
|
|
355
|
+
#
|
|
356
|
+
# For example:
|
|
357
|
+
#
|
|
358
|
+
# select("post", "person_id", Person.all.collect { |p| [ p.name, p.id ] }, { include_blank: true })
|
|
359
|
+
#
|
|
360
|
+
# would become:
|
|
361
|
+
#
|
|
362
|
+
# <select name="post[person_id]" id="post_person_id">
|
|
363
|
+
# <option value=""></option>
|
|
364
|
+
# <option value="1" selected="selected">David</option>
|
|
365
|
+
# <option value="2">Eileen</option>
|
|
366
|
+
# <option value="3">Rafael</option>
|
|
367
|
+
# </select>
|
|
368
|
+
#
|
|
369
|
+
# assuming the associated person has ID 1.
|
|
370
|
+
#
|
|
371
|
+
# This can be used to provide a default set of options in the standard way: before rendering the create form, a
|
|
372
|
+
# new model instance is assigned the default options and bound to @model_name. Usually this model is not saved
|
|
373
|
+
# to the database. Instead, a second model object is created when the create request is received.
|
|
374
|
+
# This allows the user to submit a form page more than once with the expected results of creating multiple records.
|
|
375
|
+
# In addition, this allows a single partial to be used to generate form inputs for both edit and create forms.
|
|
376
|
+
#
|
|
377
|
+
# By default, <tt>post.person_id</tt> is the selected option. Specify <tt>selected: value</tt> to use a different selection
|
|
378
|
+
# or <tt>selected: nil</tt> to leave all options unselected. Similarly, you can specify values to be disabled in the option
|
|
379
|
+
# tags by specifying the <tt>:disabled</tt> option. This can either be a single value or an array of values to be disabled.
|
|
380
|
+
#
|
|
381
|
+
# A block can be passed to +select+ to customize how the options tags will be rendered. This
|
|
382
|
+
# is useful when the options tag has complex attributes.
|
|
383
|
+
#
|
|
384
|
+
# select(report, "campaign_ids") do
|
|
385
|
+
# available_campaigns.each do |c|
|
|
386
|
+
# content_tag(:option, c.name, value: c.id, data: { tags: c.tags.to_json })
|
|
387
|
+
# end
|
|
388
|
+
# end
|
|
389
|
+
#
|
|
390
|
+
# ==== Gotcha
|
|
391
|
+
#
|
|
392
|
+
# The HTML specification says when +multiple+ parameter passed to select and all options got deselected
|
|
393
|
+
# web browsers do not send any value to server. Unfortunately this introduces a gotcha:
|
|
394
|
+
# if an +User+ model has many +roles+ and have +role_ids+ accessor, and in the form that edits roles of the user
|
|
395
|
+
# the user deselects all roles from +role_ids+ multiple select box, no +role_ids+ parameter is sent. So,
|
|
396
|
+
# any mass-assignment idiom like
|
|
397
|
+
#
|
|
398
|
+
# @user.update(params[:user])
|
|
399
|
+
#
|
|
400
|
+
# wouldn't update roles.
|
|
401
|
+
#
|
|
402
|
+
# To prevent this the helper generates an auxiliary hidden field before
|
|
403
|
+
# every multiple select. The hidden field has the same name as multiple select and blank value.
|
|
404
|
+
#
|
|
405
|
+
# <b>Note:</b> The client either sends only the hidden field (representing
|
|
406
|
+
# the deselected multiple select box), or both fields. This means that the resulting array
|
|
407
|
+
# always contains a blank string.
|
|
408
|
+
#
|
|
409
|
+
# In case if you don't want the helper to generate this hidden field you can specify
|
|
410
|
+
# <tt>include_hidden: false</tt> option.
|
|
411
|
+
#
|
|
412
|
+
def select: (untyped object, untyped method, ?untyped? choices, ?::Hash[untyped, untyped] options, ?::Hash[untyped, untyped] html_options) ?{ () -> untyped } -> untyped
|
|
413
|
+
|
|
414
|
+
# Returns radio button tags for the collection of existing return values
|
|
415
|
+
# of +method+ for +object+'s class. The value returned from calling
|
|
416
|
+
# +method+ on the instance +object+ will be selected. If calling +method+
|
|
417
|
+
# returns +nil+, no selection is made.
|
|
418
|
+
#
|
|
419
|
+
# The <tt>:value_method</tt> and <tt>:text_method</tt> parameters are
|
|
420
|
+
# methods to be called on each member of +collection+. The return values
|
|
421
|
+
# are used as the +value+ attribute and contents of each radio button tag,
|
|
422
|
+
# respectively. They can also be any object that responds to +call+, such
|
|
423
|
+
# as a +proc+, that will be called for each member of the +collection+ to
|
|
424
|
+
# retrieve the value/text.
|
|
425
|
+
#
|
|
426
|
+
# Example object structure for use with this method:
|
|
427
|
+
# class Post < ActiveRecord::Base
|
|
428
|
+
# belongs_to :author
|
|
429
|
+
# end
|
|
430
|
+
# class Author < ActiveRecord::Base
|
|
431
|
+
# has_many :posts
|
|
432
|
+
# def name_with_initial
|
|
433
|
+
# "#{first_name.first}. #{last_name}"
|
|
434
|
+
# end
|
|
435
|
+
# end
|
|
436
|
+
#
|
|
437
|
+
# Sample usage (selecting the associated Author for an instance of Post, <tt>@post</tt>):
|
|
438
|
+
# collection_radio_buttons(:post, :author_id, Author.all, :id, :name_with_initial)
|
|
439
|
+
#
|
|
440
|
+
# If <tt>@post.author_id</tt> is already <tt>1</tt>, this would return:
|
|
441
|
+
# <input id="post_author_id_1" name="post[author_id]" type="radio" value="1" checked="checked" />
|
|
442
|
+
# <label for="post_author_id_1">D. Heinemeier Hansson</label>
|
|
443
|
+
# <input id="post_author_id_2" name="post[author_id]" type="radio" value="2" />
|
|
444
|
+
# <label for="post_author_id_2">D. Thomas</label>
|
|
445
|
+
# <input id="post_author_id_3" name="post[author_id]" type="radio" value="3" />
|
|
446
|
+
# <label for="post_author_id_3">M. Clark</label>
|
|
447
|
+
#
|
|
448
|
+
# It is also possible to customize the way the elements will be shown by
|
|
449
|
+
# giving a block to the method:
|
|
450
|
+
# collection_radio_buttons(:post, :author_id, Author.all, :id, :name_with_initial) do |b|
|
|
451
|
+
# b.label { b.radio_button }
|
|
452
|
+
# end
|
|
453
|
+
#
|
|
454
|
+
# The argument passed to the block is a special kind of builder for this
|
|
455
|
+
# collection, which has the ability to generate the label and radio button
|
|
456
|
+
# for the current item in the collection, with proper text and value.
|
|
457
|
+
# Using it, you can change the label and radio button display order or
|
|
458
|
+
# even use the label as wrapper, as in the example above.
|
|
459
|
+
#
|
|
460
|
+
# The builder methods <tt>label</tt> and <tt>radio_button</tt> also accept
|
|
461
|
+
# extra HTML options:
|
|
462
|
+
# collection_radio_buttons(:post, :author_id, Author.all, :id, :name_with_initial) do |b|
|
|
463
|
+
# b.label(class: "radio_button") { b.radio_button(class: "radio_button") }
|
|
464
|
+
# end
|
|
465
|
+
#
|
|
466
|
+
# There are also three special methods available: <tt>object</tt>, <tt>text</tt> and
|
|
467
|
+
# <tt>value</tt>, which are the current item being rendered, its text and value methods,
|
|
468
|
+
# respectively. You can use them like this:
|
|
469
|
+
# collection_radio_buttons(:post, :author_id, Author.all, :id, :name_with_initial) do |b|
|
|
470
|
+
# b.label(:"data-value" => b.value) { b.radio_button + b.text }
|
|
471
|
+
# end
|
|
472
|
+
#
|
|
473
|
+
# ==== Gotcha
|
|
474
|
+
#
|
|
475
|
+
# The HTML specification says when nothing is selected on a collection of radio buttons
|
|
476
|
+
# web browsers do not send any value to server.
|
|
477
|
+
# Unfortunately this introduces a gotcha:
|
|
478
|
+
# if a +User+ model has a +category_id+ field and in the form no category is selected, no +category_id+ parameter is sent. So,
|
|
479
|
+
# any strong parameters idiom like:
|
|
480
|
+
#
|
|
481
|
+
# params.require(:user).permit(...)
|
|
482
|
+
#
|
|
483
|
+
# will raise an error since no <tt>{user: ...}</tt> will be present.
|
|
484
|
+
#
|
|
485
|
+
# To prevent this the helper generates an auxiliary hidden field before
|
|
486
|
+
# every collection of radio buttons. The hidden field has the same name as collection radio button and blank value.
|
|
487
|
+
#
|
|
488
|
+
# In case if you don't want the helper to generate this hidden field you can specify
|
|
489
|
+
# <tt>include_hidden: false</tt> option.
|
|
490
|
+
def collection_radio_buttons: (untyped object, untyped method, untyped collection, untyped value_method, untyped text_method, ?::Hash[untyped, untyped] options, ?::Hash[untyped, untyped] html_options) ?{ (ActionView::Helpers::Tags::CollectionRadioButtons::RadioButtonBuilder) -> String } -> String
|
|
491
|
+
|
|
492
|
+
# Returns check box tags for the collection of existing return values of
|
|
493
|
+
# +method+ for +object+'s class. The value returned from calling +method+
|
|
494
|
+
# on the instance +object+ will be selected. If calling +method+ returns
|
|
495
|
+
# +nil+, no selection is made.
|
|
496
|
+
#
|
|
497
|
+
# The <tt>:value_method</tt> and <tt>:text_method</tt> parameters are
|
|
498
|
+
# methods to be called on each member of +collection+. The return values
|
|
499
|
+
# are used as the +value+ attribute and contents of each check box tag,
|
|
500
|
+
# respectively. They can also be any object that responds to +call+, such
|
|
501
|
+
# as a +proc+, that will be called for each member of the +collection+ to
|
|
502
|
+
# retrieve the value/text.
|
|
503
|
+
#
|
|
504
|
+
# Example object structure for use with this method:
|
|
505
|
+
# class Post < ActiveRecord::Base
|
|
506
|
+
# has_and_belongs_to_many :authors
|
|
507
|
+
# end
|
|
508
|
+
# class Author < ActiveRecord::Base
|
|
509
|
+
# has_and_belongs_to_many :posts
|
|
510
|
+
# def name_with_initial
|
|
511
|
+
# "#{first_name.first}. #{last_name}"
|
|
512
|
+
# end
|
|
513
|
+
# end
|
|
514
|
+
#
|
|
515
|
+
# Sample usage (selecting the associated Author for an instance of Post, <tt>@post</tt>):
|
|
516
|
+
# collection_check_boxes(:post, :author_ids, Author.all, :id, :name_with_initial)
|
|
517
|
+
#
|
|
518
|
+
# If <tt>@post.author_ids</tt> is already <tt>[1]</tt>, this would return:
|
|
519
|
+
# <input id="post_author_ids_1" name="post[author_ids][]" type="checkbox" value="1" checked="checked" />
|
|
520
|
+
# <label for="post_author_ids_1">D. Heinemeier Hansson</label>
|
|
521
|
+
# <input id="post_author_ids_2" name="post[author_ids][]" type="checkbox" value="2" />
|
|
522
|
+
# <label for="post_author_ids_2">D. Thomas</label>
|
|
523
|
+
# <input id="post_author_ids_3" name="post[author_ids][]" type="checkbox" value="3" />
|
|
524
|
+
# <label for="post_author_ids_3">M. Clark</label>
|
|
525
|
+
# <input name="post[author_ids][]" type="hidden" value="" />
|
|
526
|
+
#
|
|
527
|
+
# It is also possible to customize the way the elements will be shown by
|
|
528
|
+
# giving a block to the method:
|
|
529
|
+
# collection_check_boxes(:post, :author_ids, Author.all, :id, :name_with_initial) do |b|
|
|
530
|
+
# b.label { b.check_box }
|
|
531
|
+
# end
|
|
532
|
+
#
|
|
533
|
+
# The argument passed to the block is a special kind of builder for this
|
|
534
|
+
# collection, which has the ability to generate the label and check box
|
|
535
|
+
# for the current item in the collection, with proper text and value.
|
|
536
|
+
# Using it, you can change the label and check box display order or even
|
|
537
|
+
# use the label as wrapper, as in the example above.
|
|
538
|
+
#
|
|
539
|
+
# The builder methods <tt>label</tt> and <tt>check_box</tt> also accept
|
|
540
|
+
# extra HTML options:
|
|
541
|
+
# collection_check_boxes(:post, :author_ids, Author.all, :id, :name_with_initial) do |b|
|
|
542
|
+
# b.label(class: "check_box") { b.check_box(class: "check_box") }
|
|
543
|
+
# end
|
|
544
|
+
#
|
|
545
|
+
# There are also three special methods available: <tt>object</tt>, <tt>text</tt> and
|
|
546
|
+
# <tt>value</tt>, which are the current item being rendered, its text and value methods,
|
|
547
|
+
# respectively. You can use them like this:
|
|
548
|
+
# collection_check_boxes(:post, :author_ids, Author.all, :id, :name_with_initial) do |b|
|
|
549
|
+
# b.label(:"data-value" => b.value) { b.check_box + b.text }
|
|
550
|
+
# end
|
|
551
|
+
#
|
|
552
|
+
# ==== Gotcha
|
|
553
|
+
#
|
|
554
|
+
# When no selection is made for a collection of checkboxes most
|
|
555
|
+
# web browsers will not send any value.
|
|
556
|
+
#
|
|
557
|
+
# For example, if we have a +User+ model with +category_ids+ field and we
|
|
558
|
+
# have the following code in our update action:
|
|
559
|
+
#
|
|
560
|
+
# @user.update(params[:user])
|
|
561
|
+
#
|
|
562
|
+
# If no +category_ids+ are selected then we can safely assume this field
|
|
563
|
+
# will not be updated.
|
|
564
|
+
#
|
|
565
|
+
# This is possible thanks to a hidden field generated by the helper method
|
|
566
|
+
# for every collection of checkboxes.
|
|
567
|
+
# This hidden field is given the same field name as the checkboxes with a
|
|
568
|
+
# blank value.
|
|
569
|
+
#
|
|
570
|
+
# In the rare case you don't want this hidden field, you can pass the
|
|
571
|
+
# <tt>include_hidden: false</tt> option to the helper method.
|
|
572
|
+
def collection_check_boxes: (untyped object, untyped method, untyped collection, untyped value_method, untyped text_method, ?::Hash[untyped, untyped] options, ?::Hash[untyped, untyped] html_options) ?{ (ActionView::Helpers::Tags::CollectionCheckBoxes::CheckBoxBuilder) -> String } -> String
|
|
573
|
+
end
|
|
574
|
+
end
|
|
575
|
+
end
|
|
576
|
+
|
|
577
|
+
module ActionView
|
|
578
|
+
module Helpers
|
|
579
|
+
module FormTagHelper
|
|
580
|
+
# Creates a label element. Accepts a block.
|
|
581
|
+
#
|
|
582
|
+
# ==== Options
|
|
583
|
+
# * Creates standard HTML attributes for the tag.
|
|
584
|
+
#
|
|
585
|
+
# ==== Examples
|
|
586
|
+
# label_tag 'name'
|
|
587
|
+
# # => <label for="name">Name</label>
|
|
588
|
+
#
|
|
589
|
+
# label_tag 'name', 'Your name'
|
|
590
|
+
# # => <label for="name">Your name</label>
|
|
591
|
+
#
|
|
592
|
+
# label_tag 'name', nil, class: 'small_label'
|
|
593
|
+
# # => <label for="name" class="small_label">Name</label>
|
|
594
|
+
def label_tag: (?untyped? name, ?untyped? content_or_options, ?untyped? options) ?{ () -> untyped } -> untyped
|
|
595
|
+
|
|
596
|
+
# Creates a button element that defines a <tt>submit</tt> button,
|
|
597
|
+
# <tt>reset</tt> button or a generic button which can be used in
|
|
598
|
+
# JavaScript, for example. You can use the button tag as a regular
|
|
599
|
+
# submit tag but it isn't supported in legacy browsers. However,
|
|
600
|
+
# the button tag does allow for richer labels such as images and emphasis,
|
|
601
|
+
# so this helper will also accept a block. By default, it will create
|
|
602
|
+
# a button tag with type <tt>submit</tt>, if type is not given.
|
|
603
|
+
#
|
|
604
|
+
# ==== Options
|
|
605
|
+
# * <tt>:data</tt> - This option can be used to add custom data attributes.
|
|
606
|
+
# * <tt>:disabled</tt> - If true, the user will not be able to
|
|
607
|
+
# use this input.
|
|
608
|
+
# * Any other key creates standard HTML options for the tag.
|
|
609
|
+
#
|
|
610
|
+
# ==== Data attributes
|
|
611
|
+
#
|
|
612
|
+
# * <tt>confirm: 'question?'</tt> - If present, the
|
|
613
|
+
# unobtrusive JavaScript drivers will provide a prompt with
|
|
614
|
+
# the question specified. If the user accepts, the form is
|
|
615
|
+
# processed normally, otherwise no action is taken.
|
|
616
|
+
# * <tt>:disable_with</tt> - Value of this parameter will be
|
|
617
|
+
# used as the value for a disabled version of the submit
|
|
618
|
+
# button when the form is submitted. This feature is provided
|
|
619
|
+
# by the unobtrusive JavaScript driver.
|
|
620
|
+
#
|
|
621
|
+
# ==== Examples
|
|
622
|
+
# button_tag
|
|
623
|
+
# # => <button name="button" type="submit">Button</button>
|
|
624
|
+
#
|
|
625
|
+
# button_tag 'Reset', type: 'reset'
|
|
626
|
+
# # => <button name="button" type="reset">Reset</button>
|
|
627
|
+
#
|
|
628
|
+
# button_tag 'Button', type: 'button'
|
|
629
|
+
# # => <button name="button" type="button">Button</button>
|
|
630
|
+
#
|
|
631
|
+
# button_tag 'Reset', type: 'reset', disabled: true
|
|
632
|
+
# # => <button name="button" type="reset" disabled="disabled">Reset</button>
|
|
633
|
+
#
|
|
634
|
+
# button_tag(type: 'button') do
|
|
635
|
+
# content_tag(:strong, 'Ask me!')
|
|
636
|
+
# end
|
|
637
|
+
# # => <button name="button" type="button">
|
|
638
|
+
# # <strong>Ask me!</strong>
|
|
639
|
+
# # </button>
|
|
640
|
+
#
|
|
641
|
+
# button_tag "Save", data: { confirm: "Are you sure?" }
|
|
642
|
+
# # => <button name="button" type="submit" data-confirm="Are you sure?">Save</button>
|
|
643
|
+
#
|
|
644
|
+
# button_tag "Checkout", data: { disable_with: "Please wait..." }
|
|
645
|
+
# # => <button data-disable-with="Please wait..." name="button" type="submit">Checkout</button>
|
|
646
|
+
#
|
|
647
|
+
def button_tag: (?untyped? content_or_options, ?untyped? options) ?{ () -> untyped } -> untyped
|
|
648
|
+
end
|
|
649
|
+
end
|
|
650
|
+
end
|
|
651
|
+
|
|
652
|
+
module ActionView
|
|
653
|
+
module Helpers
|
|
654
|
+
module FormHelper
|
|
655
|
+
# Creates a scope around a specific model object like form_for, but
|
|
656
|
+
# doesn't create the form tags themselves. This makes fields_for suitable
|
|
657
|
+
# for specifying additional model objects in the same form.
|
|
658
|
+
#
|
|
659
|
+
# Although the usage and purpose of +fields_for+ is similar to +form_for+'s,
|
|
660
|
+
# its method signature is slightly different. Like +form_for+, it yields
|
|
661
|
+
# a FormBuilder object associated with a particular model object to a block,
|
|
662
|
+
# and within the block allows methods to be called on the builder to
|
|
663
|
+
# generate fields associated with the model object. Fields may reflect
|
|
664
|
+
# a model object in two ways - how they are named (hence how submitted
|
|
665
|
+
# values appear within the +params+ hash in the controller) and what
|
|
666
|
+
# default values are shown when the form the fields appear in is first
|
|
667
|
+
# displayed. In order for both of these features to be specified independently,
|
|
668
|
+
# both an object name (represented by either a symbol or string) and the
|
|
669
|
+
# object itself can be passed to the method separately -
|
|
670
|
+
#
|
|
671
|
+
# <%= form_for @person do |person_form| %>
|
|
672
|
+
# First name: <%= person_form.text_field :first_name %>
|
|
673
|
+
# Last name : <%= person_form.text_field :last_name %>
|
|
674
|
+
#
|
|
675
|
+
# <%= fields_for :permission, @person.permission do |permission_fields| %>
|
|
676
|
+
# Admin? : <%= permission_fields.check_box :admin %>
|
|
677
|
+
# <% end %>
|
|
678
|
+
#
|
|
679
|
+
# <%= person_form.submit %>
|
|
680
|
+
# <% end %>
|
|
681
|
+
#
|
|
682
|
+
# In this case, the checkbox field will be represented by an HTML +input+
|
|
683
|
+
# tag with the +name+ attribute <tt>permission[admin]</tt>, and the submitted
|
|
684
|
+
# value will appear in the controller as <tt>params[:permission][:admin]</tt>.
|
|
685
|
+
# If <tt>@person.permission</tt> is an existing record with an attribute
|
|
686
|
+
# +admin+, the initial state of the checkbox when first displayed will
|
|
687
|
+
# reflect the value of <tt>@person.permission.admin</tt>.
|
|
688
|
+
#
|
|
689
|
+
# Often this can be simplified by passing just the name of the model
|
|
690
|
+
# object to +fields_for+ -
|
|
691
|
+
#
|
|
692
|
+
# <%= fields_for :permission do |permission_fields| %>
|
|
693
|
+
# Admin?: <%= permission_fields.check_box :admin %>
|
|
694
|
+
# <% end %>
|
|
695
|
+
#
|
|
696
|
+
# ...in which case, if <tt>:permission</tt> also happens to be the name of an
|
|
697
|
+
# instance variable <tt>@permission</tt>, the initial state of the input
|
|
698
|
+
# field will reflect the value of that variable's attribute <tt>@permission.admin</tt>.
|
|
699
|
+
#
|
|
700
|
+
# Alternatively, you can pass just the model object itself (if the first
|
|
701
|
+
# argument isn't a string or symbol +fields_for+ will realize that the
|
|
702
|
+
# name has been omitted) -
|
|
703
|
+
#
|
|
704
|
+
# <%= fields_for @person.permission do |permission_fields| %>
|
|
705
|
+
# Admin?: <%= permission_fields.check_box :admin %>
|
|
706
|
+
# <% end %>
|
|
707
|
+
#
|
|
708
|
+
# and +fields_for+ will derive the required name of the field from the
|
|
709
|
+
# _class_ of the model object, e.g. if <tt>@person.permission</tt>, is
|
|
710
|
+
# of class +Permission+, the field will still be named <tt>permission[admin]</tt>.
|
|
711
|
+
#
|
|
712
|
+
# Note: This also works for the methods in FormOptionsHelper and
|
|
713
|
+
# DateHelper that are designed to work with an object as base, like
|
|
714
|
+
# FormOptionsHelper#collection_select and DateHelper#datetime_select.
|
|
715
|
+
#
|
|
716
|
+
# === Nested Attributes Examples
|
|
717
|
+
#
|
|
718
|
+
# When the object belonging to the current scope has a nested attribute
|
|
719
|
+
# writer for a certain attribute, fields_for will yield a new scope
|
|
720
|
+
# for that attribute. This allows you to create forms that set or change
|
|
721
|
+
# the attributes of a parent object and its associations in one go.
|
|
722
|
+
#
|
|
723
|
+
# Nested attribute writers are normal setter methods named after an
|
|
724
|
+
# association. The most common way of defining these writers is either
|
|
725
|
+
# with +accepts_nested_attributes_for+ in a model definition or by
|
|
726
|
+
# defining a method with the proper name. For example: the attribute
|
|
727
|
+
# writer for the association <tt>:address</tt> is called
|
|
728
|
+
# <tt>address_attributes=</tt>.
|
|
729
|
+
#
|
|
730
|
+
# Whether a one-to-one or one-to-many style form builder will be yielded
|
|
731
|
+
# depends on whether the normal reader method returns a _single_ object
|
|
732
|
+
# or an _array_ of objects.
|
|
733
|
+
#
|
|
734
|
+
# ==== One-to-one
|
|
735
|
+
#
|
|
736
|
+
# Consider a Person class which returns a _single_ Address from the
|
|
737
|
+
# <tt>address</tt> reader method and responds to the
|
|
738
|
+
# <tt>address_attributes=</tt> writer method:
|
|
739
|
+
#
|
|
740
|
+
# class Person
|
|
741
|
+
# def address
|
|
742
|
+
# @address
|
|
743
|
+
# end
|
|
744
|
+
#
|
|
745
|
+
# def address_attributes=(attributes)
|
|
746
|
+
# # Process the attributes hash
|
|
747
|
+
# end
|
|
748
|
+
# end
|
|
749
|
+
#
|
|
750
|
+
# This model can now be used with a nested fields_for, like so:
|
|
751
|
+
#
|
|
752
|
+
# <%= form_for @person do |person_form| %>
|
|
753
|
+
# ...
|
|
754
|
+
# <%= person_form.fields_for :address do |address_fields| %>
|
|
755
|
+
# Street : <%= address_fields.text_field :street %>
|
|
756
|
+
# Zip code: <%= address_fields.text_field :zip_code %>
|
|
757
|
+
# <% end %>
|
|
758
|
+
# ...
|
|
759
|
+
# <% end %>
|
|
760
|
+
#
|
|
761
|
+
# When address is already an association on a Person you can use
|
|
762
|
+
# +accepts_nested_attributes_for+ to define the writer method for you:
|
|
763
|
+
#
|
|
764
|
+
# class Person < ActiveRecord::Base
|
|
765
|
+
# has_one :address
|
|
766
|
+
# accepts_nested_attributes_for :address
|
|
767
|
+
# end
|
|
768
|
+
#
|
|
769
|
+
# If you want to destroy the associated model through the form, you have
|
|
770
|
+
# to enable it first using the <tt>:allow_destroy</tt> option for
|
|
771
|
+
# +accepts_nested_attributes_for+:
|
|
772
|
+
#
|
|
773
|
+
# class Person < ActiveRecord::Base
|
|
774
|
+
# has_one :address
|
|
775
|
+
# accepts_nested_attributes_for :address, allow_destroy: true
|
|
776
|
+
# end
|
|
777
|
+
#
|
|
778
|
+
# Now, when you use a form element with the <tt>_destroy</tt> parameter,
|
|
779
|
+
# with a value that evaluates to +true+, you will destroy the associated
|
|
780
|
+
# model (eg. 1, '1', true, or 'true'):
|
|
781
|
+
#
|
|
782
|
+
# <%= form_for @person do |person_form| %>
|
|
783
|
+
# ...
|
|
784
|
+
# <%= person_form.fields_for :address do |address_fields| %>
|
|
785
|
+
# ...
|
|
786
|
+
# Delete: <%= address_fields.check_box :_destroy %>
|
|
787
|
+
# <% end %>
|
|
788
|
+
# ...
|
|
789
|
+
# <% end %>
|
|
790
|
+
#
|
|
791
|
+
# ==== One-to-many
|
|
792
|
+
#
|
|
793
|
+
# Consider a Person class which returns an _array_ of Project instances
|
|
794
|
+
# from the <tt>projects</tt> reader method and responds to the
|
|
795
|
+
# <tt>projects_attributes=</tt> writer method:
|
|
796
|
+
#
|
|
797
|
+
# class Person
|
|
798
|
+
# def projects
|
|
799
|
+
# [@project1, @project2]
|
|
800
|
+
# end
|
|
801
|
+
#
|
|
802
|
+
# def projects_attributes=(attributes)
|
|
803
|
+
# # Process the attributes hash
|
|
804
|
+
# end
|
|
805
|
+
# end
|
|
806
|
+
#
|
|
807
|
+
# Note that the <tt>projects_attributes=</tt> writer method is in fact
|
|
808
|
+
# required for fields_for to correctly identify <tt>:projects</tt> as a
|
|
809
|
+
# collection, and the correct indices to be set in the form markup.
|
|
810
|
+
#
|
|
811
|
+
# When projects is already an association on Person you can use
|
|
812
|
+
# +accepts_nested_attributes_for+ to define the writer method for you:
|
|
813
|
+
#
|
|
814
|
+
# class Person < ActiveRecord::Base
|
|
815
|
+
# has_many :projects
|
|
816
|
+
# accepts_nested_attributes_for :projects
|
|
817
|
+
# end
|
|
818
|
+
#
|
|
819
|
+
# This model can now be used with a nested fields_for. The block given to
|
|
820
|
+
# the nested fields_for call will be repeated for each instance in the
|
|
821
|
+
# collection:
|
|
822
|
+
#
|
|
823
|
+
# <%= form_for @person do |person_form| %>
|
|
824
|
+
# ...
|
|
825
|
+
# <%= person_form.fields_for :projects do |project_fields| %>
|
|
826
|
+
# <% if project_fields.object.active? %>
|
|
827
|
+
# Name: <%= project_fields.text_field :name %>
|
|
828
|
+
# <% end %>
|
|
829
|
+
# <% end %>
|
|
830
|
+
# ...
|
|
831
|
+
# <% end %>
|
|
832
|
+
#
|
|
833
|
+
# It's also possible to specify the instance to be used:
|
|
834
|
+
#
|
|
835
|
+
# <%= form_for @person do |person_form| %>
|
|
836
|
+
# ...
|
|
837
|
+
# <% @person.projects.each do |project| %>
|
|
838
|
+
# <% if project.active? %>
|
|
839
|
+
# <%= person_form.fields_for :projects, project do |project_fields| %>
|
|
840
|
+
# Name: <%= project_fields.text_field :name %>
|
|
841
|
+
# <% end %>
|
|
842
|
+
# <% end %>
|
|
843
|
+
# <% end %>
|
|
844
|
+
# ...
|
|
845
|
+
# <% end %>
|
|
846
|
+
#
|
|
847
|
+
# Or a collection to be used:
|
|
848
|
+
#
|
|
849
|
+
# <%= form_for @person do |person_form| %>
|
|
850
|
+
# ...
|
|
851
|
+
# <%= person_form.fields_for :projects, @active_projects do |project_fields| %>
|
|
852
|
+
# Name: <%= project_fields.text_field :name %>
|
|
853
|
+
# <% end %>
|
|
854
|
+
# ...
|
|
855
|
+
# <% end %>
|
|
856
|
+
#
|
|
857
|
+
# If you want to destroy any of the associated models through the
|
|
858
|
+
# form, you have to enable it first using the <tt>:allow_destroy</tt>
|
|
859
|
+
# option for +accepts_nested_attributes_for+:
|
|
860
|
+
#
|
|
861
|
+
# class Person < ActiveRecord::Base
|
|
862
|
+
# has_many :projects
|
|
863
|
+
# accepts_nested_attributes_for :projects, allow_destroy: true
|
|
864
|
+
# end
|
|
865
|
+
#
|
|
866
|
+
# This will allow you to specify which models to destroy in the
|
|
867
|
+
# attributes hash by adding a form element for the <tt>_destroy</tt>
|
|
868
|
+
# parameter with a value that evaluates to +true+
|
|
869
|
+
# (eg. 1, '1', true, or 'true'):
|
|
870
|
+
#
|
|
871
|
+
# <%= form_for @person do |person_form| %>
|
|
872
|
+
# ...
|
|
873
|
+
# <%= person_form.fields_for :projects do |project_fields| %>
|
|
874
|
+
# Delete: <%= project_fields.check_box :_destroy %>
|
|
875
|
+
# <% end %>
|
|
876
|
+
# ...
|
|
877
|
+
# <% end %>
|
|
878
|
+
#
|
|
879
|
+
# When a collection is used you might want to know the index of each
|
|
880
|
+
# object into the array. For this purpose, the <tt>index</tt> method
|
|
881
|
+
# is available in the FormBuilder object.
|
|
882
|
+
#
|
|
883
|
+
# <%= form_for @person do |person_form| %>
|
|
884
|
+
# ...
|
|
885
|
+
# <%= person_form.fields_for :projects do |project_fields| %>
|
|
886
|
+
# Project #<%= project_fields.index %>
|
|
887
|
+
# ...
|
|
888
|
+
# <% end %>
|
|
889
|
+
# ...
|
|
890
|
+
# <% end %>
|
|
891
|
+
#
|
|
892
|
+
# Note that fields_for will automatically generate a hidden field
|
|
893
|
+
# to store the ID of the record. There are circumstances where this
|
|
894
|
+
# hidden field is not needed and you can pass <tt>include_id: false</tt>
|
|
895
|
+
# to prevent fields_for from rendering it automatically.
|
|
896
|
+
def fields_for: (untyped record_name, ?untyped? record_object, ?::Hash[untyped, untyped] options) { (Helpers::FormBuilder) -> untyped } -> String
|
|
897
|
+
|
|
898
|
+
# Creates a form that allows the user to create or update the attributes
|
|
899
|
+
# of a specific model object.
|
|
900
|
+
#
|
|
901
|
+
# The method can be used in several slightly different ways, depending on
|
|
902
|
+
# how much you wish to rely on Rails to infer automatically from the model
|
|
903
|
+
# how the form should be constructed. For a generic model object, a form
|
|
904
|
+
# can be created by passing +form_for+ a string or symbol representing
|
|
905
|
+
# the object we are concerned with:
|
|
906
|
+
#
|
|
907
|
+
# <%= form_for :person do |f| %>
|
|
908
|
+
# First name: <%= f.text_field :first_name %><br />
|
|
909
|
+
# Last name : <%= f.text_field :last_name %><br />
|
|
910
|
+
# Biography : <%= f.text_area :biography %><br />
|
|
911
|
+
# Admin? : <%= f.check_box :admin %><br />
|
|
912
|
+
# <%= f.submit %>
|
|
913
|
+
# <% end %>
|
|
914
|
+
#
|
|
915
|
+
# The variable +f+ yielded to the block is a FormBuilder object that
|
|
916
|
+
# incorporates the knowledge about the model object represented by
|
|
917
|
+
# <tt>:person</tt> passed to +form_for+. Methods defined on the FormBuilder
|
|
918
|
+
# are used to generate fields bound to this model. Thus, for example,
|
|
919
|
+
#
|
|
920
|
+
# <%= f.text_field :first_name %>
|
|
921
|
+
#
|
|
922
|
+
# will get expanded to
|
|
923
|
+
#
|
|
924
|
+
# <%= text_field :person, :first_name %>
|
|
925
|
+
#
|
|
926
|
+
# which results in an HTML <tt><input></tt> tag whose +name+ attribute is
|
|
927
|
+
# <tt>person[first_name]</tt>. This means that when the form is submitted,
|
|
928
|
+
# the value entered by the user will be available in the controller as
|
|
929
|
+
# <tt>params[:person][:first_name]</tt>.
|
|
930
|
+
#
|
|
931
|
+
# For fields generated in this way using the FormBuilder,
|
|
932
|
+
# if <tt>:person</tt> also happens to be the name of an instance variable
|
|
933
|
+
# <tt>@person</tt>, the default value of the field shown when the form is
|
|
934
|
+
# initially displayed (e.g. in the situation where you are editing an
|
|
935
|
+
# existing record) will be the value of the corresponding attribute of
|
|
936
|
+
# <tt>@person</tt>.
|
|
937
|
+
#
|
|
938
|
+
# The rightmost argument to +form_for+ is an
|
|
939
|
+
# optional hash of options -
|
|
940
|
+
#
|
|
941
|
+
# * <tt>:url</tt> - The URL the form is to be submitted to. This may be
|
|
942
|
+
# represented in the same way as values passed to +url_for+ or +link_to+.
|
|
943
|
+
# So for example you may use a named route directly. When the model is
|
|
944
|
+
# represented by a string or symbol, as in the example above, if the
|
|
945
|
+
# <tt>:url</tt> option is not specified, by default the form will be
|
|
946
|
+
# sent back to the current URL (We will describe below an alternative
|
|
947
|
+
# resource-oriented usage of +form_for+ in which the URL does not need
|
|
948
|
+
# to be specified explicitly).
|
|
949
|
+
# * <tt>:namespace</tt> - A namespace for your form to ensure uniqueness of
|
|
950
|
+
# id attributes on form elements. The namespace attribute will be prefixed
|
|
951
|
+
# with underscore on the generated HTML id.
|
|
952
|
+
# * <tt>:method</tt> - The method to use when submitting the form, usually
|
|
953
|
+
# either "get" or "post". If "patch", "put", "delete", or another verb
|
|
954
|
+
# is used, a hidden input with name <tt>_method</tt> is added to
|
|
955
|
+
# simulate the verb over post.
|
|
956
|
+
# * <tt>:authenticity_token</tt> - Authenticity token to use in the form.
|
|
957
|
+
# Use only if you need to pass custom authenticity token string, or to
|
|
958
|
+
# not add authenticity_token field at all (by passing <tt>false</tt>).
|
|
959
|
+
# Remote forms may omit the embedded authenticity token by setting
|
|
960
|
+
# <tt>config.action_view.embed_authenticity_token_in_remote_forms = false</tt>.
|
|
961
|
+
# This is helpful when you're fragment-caching the form. Remote forms
|
|
962
|
+
# get the authenticity token from the <tt>meta</tt> tag, so embedding is
|
|
963
|
+
# unnecessary unless you support browsers without JavaScript.
|
|
964
|
+
# * <tt>:remote</tt> - If set to true, will allow the Unobtrusive
|
|
965
|
+
# JavaScript drivers to control the submit behavior. By default this
|
|
966
|
+
# behavior is an ajax submit.
|
|
967
|
+
# * <tt>:enforce_utf8</tt> - If set to false, a hidden input with name
|
|
968
|
+
# utf8 is not output.
|
|
969
|
+
# * <tt>:html</tt> - Optional HTML attributes for the form tag.
|
|
970
|
+
#
|
|
971
|
+
# Also note that +form_for+ doesn't create an exclusive scope. It's still
|
|
972
|
+
# possible to use both the stand-alone FormHelper methods and methods
|
|
973
|
+
# from FormTagHelper. For example:
|
|
974
|
+
#
|
|
975
|
+
# <%= form_for :person do |f| %>
|
|
976
|
+
# First name: <%= f.text_field :first_name %>
|
|
977
|
+
# Last name : <%= f.text_field :last_name %>
|
|
978
|
+
# Biography : <%= text_area :person, :biography %>
|
|
979
|
+
# Admin? : <%= check_box_tag "person[admin]", "1", @person.company.admin? %>
|
|
980
|
+
# <%= f.submit %>
|
|
981
|
+
# <% end %>
|
|
982
|
+
#
|
|
983
|
+
# This also works for the methods in FormOptionsHelper and DateHelper that
|
|
984
|
+
# are designed to work with an object as base, like
|
|
985
|
+
# FormOptionsHelper#collection_select and DateHelper#datetime_select.
|
|
986
|
+
#
|
|
987
|
+
# === #form_for with a model object
|
|
988
|
+
#
|
|
989
|
+
# In the examples above, the object to be created or edited was
|
|
990
|
+
# represented by a symbol passed to +form_for+, and we noted that
|
|
991
|
+
# a string can also be used equivalently. It is also possible, however,
|
|
992
|
+
# to pass a model object itself to +form_for+. For example, if <tt>@post</tt>
|
|
993
|
+
# is an existing record you wish to edit, you can create the form using
|
|
994
|
+
#
|
|
995
|
+
# <%= form_for @post do |f| %>
|
|
996
|
+
# ...
|
|
997
|
+
# <% end %>
|
|
998
|
+
#
|
|
999
|
+
# This behaves in almost the same way as outlined previously, with a
|
|
1000
|
+
# couple of small exceptions. First, the prefix used to name the input
|
|
1001
|
+
# elements within the form (hence the key that denotes them in the +params+
|
|
1002
|
+
# hash) is actually derived from the object's _class_, e.g. <tt>params[:post]</tt>
|
|
1003
|
+
# if the object's class is +Post+. However, this can be overwritten using
|
|
1004
|
+
# the <tt>:as</tt> option, e.g. -
|
|
1005
|
+
#
|
|
1006
|
+
# <%= form_for(@person, as: :client) do |f| %>
|
|
1007
|
+
# ...
|
|
1008
|
+
# <% end %>
|
|
1009
|
+
#
|
|
1010
|
+
# would result in <tt>params[:client]</tt>.
|
|
1011
|
+
#
|
|
1012
|
+
# Secondly, the field values shown when the form is initially displayed
|
|
1013
|
+
# are taken from the attributes of the object passed to +form_for+,
|
|
1014
|
+
# regardless of whether the object is an instance
|
|
1015
|
+
# variable. So, for example, if we had a _local_ variable +post+
|
|
1016
|
+
# representing an existing record,
|
|
1017
|
+
#
|
|
1018
|
+
# <%= form_for post do |f| %>
|
|
1019
|
+
# ...
|
|
1020
|
+
# <% end %>
|
|
1021
|
+
#
|
|
1022
|
+
# would produce a form with fields whose initial state reflect the current
|
|
1023
|
+
# values of the attributes of +post+.
|
|
1024
|
+
#
|
|
1025
|
+
# === Resource-oriented style
|
|
1026
|
+
#
|
|
1027
|
+
# In the examples just shown, although not indicated explicitly, we still
|
|
1028
|
+
# need to use the <tt>:url</tt> option in order to specify where the
|
|
1029
|
+
# form is going to be sent. However, further simplification is possible
|
|
1030
|
+
# if the record passed to +form_for+ is a _resource_, i.e. it corresponds
|
|
1031
|
+
# to a set of RESTful routes, e.g. defined using the +resources+ method
|
|
1032
|
+
# in <tt>config/routes.rb</tt>. In this case Rails will simply infer the
|
|
1033
|
+
# appropriate URL from the record itself. For example,
|
|
1034
|
+
#
|
|
1035
|
+
# <%= form_for @post do |f| %>
|
|
1036
|
+
# ...
|
|
1037
|
+
# <% end %>
|
|
1038
|
+
#
|
|
1039
|
+
# is then equivalent to something like:
|
|
1040
|
+
#
|
|
1041
|
+
# <%= form_for @post, as: :post, url: post_path(@post), method: :patch, html: { class: "edit_post", id: "edit_post_45" } do |f| %>
|
|
1042
|
+
# ...
|
|
1043
|
+
# <% end %>
|
|
1044
|
+
#
|
|
1045
|
+
# And for a new record
|
|
1046
|
+
#
|
|
1047
|
+
# <%= form_for(Post.new) do |f| %>
|
|
1048
|
+
# ...
|
|
1049
|
+
# <% end %>
|
|
1050
|
+
#
|
|
1051
|
+
# is equivalent to something like:
|
|
1052
|
+
#
|
|
1053
|
+
# <%= form_for @post, as: :post, url: posts_path, html: { class: "new_post", id: "new_post" } do |f| %>
|
|
1054
|
+
# ...
|
|
1055
|
+
# <% end %>
|
|
1056
|
+
#
|
|
1057
|
+
# However you can still overwrite individual conventions, such as:
|
|
1058
|
+
#
|
|
1059
|
+
# <%= form_for(@post, url: super_posts_path) do |f| %>
|
|
1060
|
+
# ...
|
|
1061
|
+
# <% end %>
|
|
1062
|
+
#
|
|
1063
|
+
# You can also set the answer format, like this:
|
|
1064
|
+
#
|
|
1065
|
+
# <%= form_for(@post, format: :json) do |f| %>
|
|
1066
|
+
# ...
|
|
1067
|
+
# <% end %>
|
|
1068
|
+
#
|
|
1069
|
+
# For namespaced routes, like +admin_post_url+:
|
|
1070
|
+
#
|
|
1071
|
+
# <%= form_for([:admin, @post]) do |f| %>
|
|
1072
|
+
# ...
|
|
1073
|
+
# <% end %>
|
|
1074
|
+
#
|
|
1075
|
+
# If your resource has associations defined, for example, you want to add comments
|
|
1076
|
+
# to the document given that the routes are set correctly:
|
|
1077
|
+
#
|
|
1078
|
+
# <%= form_for([@document, @comment]) do |f| %>
|
|
1079
|
+
# ...
|
|
1080
|
+
# <% end %>
|
|
1081
|
+
#
|
|
1082
|
+
# Where <tt>@document = Document.find(params[:id])</tt> and
|
|
1083
|
+
# <tt>@comment = Comment.new</tt>.
|
|
1084
|
+
#
|
|
1085
|
+
# === Setting the method
|
|
1086
|
+
#
|
|
1087
|
+
# You can force the form to use the full array of HTTP verbs by setting
|
|
1088
|
+
#
|
|
1089
|
+
# method: (:get|:post|:patch|:put|:delete)
|
|
1090
|
+
#
|
|
1091
|
+
# in the options hash. If the verb is not GET or POST, which are natively
|
|
1092
|
+
# supported by HTML forms, the form will be set to POST and a hidden input
|
|
1093
|
+
# called _method will carry the intended verb for the server to interpret.
|
|
1094
|
+
#
|
|
1095
|
+
# === Unobtrusive JavaScript
|
|
1096
|
+
#
|
|
1097
|
+
# Specifying:
|
|
1098
|
+
#
|
|
1099
|
+
# remote: true
|
|
1100
|
+
#
|
|
1101
|
+
# in the options hash creates a form that will allow the unobtrusive JavaScript drivers to modify its
|
|
1102
|
+
# behavior. The expected default behavior is an XMLHttpRequest in the background instead of the regular
|
|
1103
|
+
# POST arrangement, but ultimately the behavior is the choice of the JavaScript driver implementor.
|
|
1104
|
+
# Even though it's using JavaScript to serialize the form elements, the form submission will work just like
|
|
1105
|
+
# a regular submission as viewed by the receiving side (all elements available in <tt>params</tt>).
|
|
1106
|
+
#
|
|
1107
|
+
# Example:
|
|
1108
|
+
#
|
|
1109
|
+
# <%= form_for(@post, remote: true) do |f| %>
|
|
1110
|
+
# ...
|
|
1111
|
+
# <% end %>
|
|
1112
|
+
#
|
|
1113
|
+
# The HTML generated for this would be:
|
|
1114
|
+
#
|
|
1115
|
+
# <form action='http://www.example.com' method='post' data-remote='true'>
|
|
1116
|
+
# <input name='_method' type='hidden' value='patch' />
|
|
1117
|
+
# ...
|
|
1118
|
+
# </form>
|
|
1119
|
+
#
|
|
1120
|
+
# === Setting HTML options
|
|
1121
|
+
#
|
|
1122
|
+
# You can set data attributes directly by passing in a data hash, but all other HTML options must be wrapped in
|
|
1123
|
+
# the HTML key. Example:
|
|
1124
|
+
#
|
|
1125
|
+
# <%= form_for(@post, data: { behavior: "autosave" }, html: { name: "go" }) do |f| %>
|
|
1126
|
+
# ...
|
|
1127
|
+
# <% end %>
|
|
1128
|
+
#
|
|
1129
|
+
# The HTML generated for this would be:
|
|
1130
|
+
#
|
|
1131
|
+
# <form action='http://www.example.com' method='post' data-behavior='autosave' name='go'>
|
|
1132
|
+
# <input name='_method' type='hidden' value='patch' />
|
|
1133
|
+
# ...
|
|
1134
|
+
# </form>
|
|
1135
|
+
#
|
|
1136
|
+
# === Removing hidden model id's
|
|
1137
|
+
#
|
|
1138
|
+
# The form_for method automatically includes the model id as a hidden field in the form.
|
|
1139
|
+
# This is used to maintain the correlation between the form data and its associated model.
|
|
1140
|
+
# Some ORM systems do not use IDs on nested models so in this case you want to be able
|
|
1141
|
+
# to disable the hidden id.
|
|
1142
|
+
#
|
|
1143
|
+
# In the following example the Post model has many Comments stored within it in a NoSQL database,
|
|
1144
|
+
# thus there is no primary key for comments.
|
|
1145
|
+
#
|
|
1146
|
+
# Example:
|
|
1147
|
+
#
|
|
1148
|
+
# <%= form_for(@post) do |f| %>
|
|
1149
|
+
# <%= f.fields_for(:comments, include_id: false) do |cf| %>
|
|
1150
|
+
# ...
|
|
1151
|
+
# <% end %>
|
|
1152
|
+
# <% end %>
|
|
1153
|
+
#
|
|
1154
|
+
# === Customized form builders
|
|
1155
|
+
#
|
|
1156
|
+
# You can also build forms using a customized FormBuilder class. Subclass
|
|
1157
|
+
# FormBuilder and override or define some more helpers, then use your
|
|
1158
|
+
# custom builder. For example, let's say you made a helper to
|
|
1159
|
+
# automatically add labels to form inputs.
|
|
1160
|
+
#
|
|
1161
|
+
# <%= form_for @person, url: { action: "create" }, builder: LabellingFormBuilder do |f| %>
|
|
1162
|
+
# <%= f.text_field :first_name %>
|
|
1163
|
+
# <%= f.text_field :last_name %>
|
|
1164
|
+
# <%= f.text_area :biography %>
|
|
1165
|
+
# <%= f.check_box :admin %>
|
|
1166
|
+
# <%= f.submit %>
|
|
1167
|
+
# <% end %>
|
|
1168
|
+
#
|
|
1169
|
+
# In this case, if you use this:
|
|
1170
|
+
#
|
|
1171
|
+
# <%= render f %>
|
|
1172
|
+
#
|
|
1173
|
+
# The rendered template is <tt>people/_labelling_form</tt> and the local
|
|
1174
|
+
# variable referencing the form builder is called
|
|
1175
|
+
# <tt>labelling_form</tt>.
|
|
1176
|
+
#
|
|
1177
|
+
# The custom FormBuilder class is automatically merged with the options
|
|
1178
|
+
# of a nested fields_for call, unless it's explicitly set.
|
|
1179
|
+
#
|
|
1180
|
+
# In many cases you will want to wrap the above in another helper, so you
|
|
1181
|
+
# could do something like the following:
|
|
1182
|
+
#
|
|
1183
|
+
# def labelled_form_for(record_or_name_or_array, *args, &block)
|
|
1184
|
+
# options = args.extract_options!
|
|
1185
|
+
# form_for(record_or_name_or_array, *(args << options.merge(builder: LabellingFormBuilder)), &block)
|
|
1186
|
+
# end
|
|
1187
|
+
#
|
|
1188
|
+
# If you don't need to attach a form to a model instance, then check out
|
|
1189
|
+
# FormTagHelper#form_tag.
|
|
1190
|
+
#
|
|
1191
|
+
# === Form to external resources
|
|
1192
|
+
#
|
|
1193
|
+
# When you build forms to external resources sometimes you need to set an authenticity token or just render a form
|
|
1194
|
+
# without it, for example when you submit data to a payment gateway number and types of fields could be limited.
|
|
1195
|
+
#
|
|
1196
|
+
# To set an authenticity token you need to pass an <tt>:authenticity_token</tt> parameter
|
|
1197
|
+
#
|
|
1198
|
+
# <%= form_for @invoice, url: external_url, authenticity_token: 'external_token' do |f| %>
|
|
1199
|
+
# ...
|
|
1200
|
+
# <% end %>
|
|
1201
|
+
#
|
|
1202
|
+
# If you don't want to an authenticity token field be rendered at all just pass <tt>false</tt>:
|
|
1203
|
+
#
|
|
1204
|
+
# <%= form_for @invoice, url: external_url, authenticity_token: false do |f| %>
|
|
1205
|
+
# ...
|
|
1206
|
+
# <% end %>
|
|
1207
|
+
def form_for: (untyped record, ?::Hash[untyped, untyped] options) { (Helpers::FormBuilder) -> untyped } -> String
|
|
1208
|
+
|
|
1209
|
+
# Creates a form tag based on mixing URLs, scopes, or models.
|
|
1210
|
+
#
|
|
1211
|
+
# # Using just a URL:
|
|
1212
|
+
# <%= form_with url: posts_path do |form| %>
|
|
1213
|
+
# <%= form.text_field :title %>
|
|
1214
|
+
# <% end %>
|
|
1215
|
+
# # =>
|
|
1216
|
+
# <form action="/posts" method="post" data-remote="true">
|
|
1217
|
+
# <input type="text" name="title">
|
|
1218
|
+
# </form>
|
|
1219
|
+
#
|
|
1220
|
+
# # Adding a scope prefixes the input field names:
|
|
1221
|
+
# <%= form_with scope: :post, url: posts_path do |form| %>
|
|
1222
|
+
# <%= form.text_field :title %>
|
|
1223
|
+
# <% end %>
|
|
1224
|
+
# # =>
|
|
1225
|
+
# <form action="/posts" method="post" data-remote="true">
|
|
1226
|
+
# <input type="text" name="post[title]">
|
|
1227
|
+
# </form>
|
|
1228
|
+
#
|
|
1229
|
+
# # Using a model infers both the URL and scope:
|
|
1230
|
+
# <%= form_with model: Post.new do |form| %>
|
|
1231
|
+
# <%= form.text_field :title %>
|
|
1232
|
+
# <% end %>
|
|
1233
|
+
# # =>
|
|
1234
|
+
# <form action="/posts" method="post" data-remote="true">
|
|
1235
|
+
# <input type="text" name="post[title]">
|
|
1236
|
+
# </form>
|
|
1237
|
+
#
|
|
1238
|
+
# # An existing model makes an update form and fills out field values:
|
|
1239
|
+
# <%= form_with model: Post.first do |form| %>
|
|
1240
|
+
# <%= form.text_field :title %>
|
|
1241
|
+
# <% end %>
|
|
1242
|
+
# # =>
|
|
1243
|
+
# <form action="/posts/1" method="post" data-remote="true">
|
|
1244
|
+
# <input type="hidden" name="_method" value="patch">
|
|
1245
|
+
# <input type="text" name="post[title]" value="<the title of the post>">
|
|
1246
|
+
# </form>
|
|
1247
|
+
#
|
|
1248
|
+
# # Though the fields don't have to correspond to model attributes:
|
|
1249
|
+
# <%= form_with model: Cat.new do |form| %>
|
|
1250
|
+
# <%= form.text_field :cats_dont_have_gills %>
|
|
1251
|
+
# <%= form.text_field :but_in_forms_they_can %>
|
|
1252
|
+
# <% end %>
|
|
1253
|
+
# # =>
|
|
1254
|
+
# <form action="/cats" method="post" data-remote="true">
|
|
1255
|
+
# <input type="text" name="cat[cats_dont_have_gills]">
|
|
1256
|
+
# <input type="text" name="cat[but_in_forms_they_can]">
|
|
1257
|
+
# </form>
|
|
1258
|
+
#
|
|
1259
|
+
# The parameters in the forms are accessible in controllers according to
|
|
1260
|
+
# their name nesting. So inputs named +title+ and <tt>post[title]</tt> are
|
|
1261
|
+
# accessible as <tt>params[:title]</tt> and <tt>params[:post][:title]</tt>
|
|
1262
|
+
# respectively.
|
|
1263
|
+
#
|
|
1264
|
+
# By default +form_with+ attaches the <tt>data-remote</tt> attribute
|
|
1265
|
+
# submitting the form via an XMLHTTPRequest in the background if an
|
|
1266
|
+
# Unobtrusive JavaScript driver, like rails-ujs, is used. See the
|
|
1267
|
+
# <tt>:local</tt> option for more.
|
|
1268
|
+
#
|
|
1269
|
+
# For ease of comparison the examples above left out the submit button,
|
|
1270
|
+
# as well as the auto generated hidden fields that enable UTF-8 support
|
|
1271
|
+
# and adds an authenticity token needed for cross site request forgery
|
|
1272
|
+
# protection.
|
|
1273
|
+
#
|
|
1274
|
+
# === Resource-oriented style
|
|
1275
|
+
#
|
|
1276
|
+
# In many of the examples just shown, the +:model+ passed to +form_with+
|
|
1277
|
+
# is a _resource_. It corresponds to a set of RESTful routes, most likely
|
|
1278
|
+
# defined via +resources+ in <tt>config/routes.rb</tt>.
|
|
1279
|
+
#
|
|
1280
|
+
# So when passing such a model record, Rails infers the URL and method.
|
|
1281
|
+
#
|
|
1282
|
+
# <%= form_with model: @post do |form| %>
|
|
1283
|
+
# ...
|
|
1284
|
+
# <% end %>
|
|
1285
|
+
#
|
|
1286
|
+
# is then equivalent to something like:
|
|
1287
|
+
#
|
|
1288
|
+
# <%= form_with scope: :post, url: post_path(@post), method: :patch do |form| %>
|
|
1289
|
+
# ...
|
|
1290
|
+
# <% end %>
|
|
1291
|
+
#
|
|
1292
|
+
# And for a new record
|
|
1293
|
+
#
|
|
1294
|
+
# <%= form_with model: Post.new do |form| %>
|
|
1295
|
+
# ...
|
|
1296
|
+
# <% end %>
|
|
1297
|
+
#
|
|
1298
|
+
# is equivalent to something like:
|
|
1299
|
+
#
|
|
1300
|
+
# <%= form_with scope: :post, url: posts_path do |form| %>
|
|
1301
|
+
# ...
|
|
1302
|
+
# <% end %>
|
|
1303
|
+
#
|
|
1304
|
+
# ==== +form_with+ options
|
|
1305
|
+
#
|
|
1306
|
+
# * <tt>:url</tt> - The URL the form submits to. Akin to values passed to
|
|
1307
|
+
# +url_for+ or +link_to+. For example, you may use a named route
|
|
1308
|
+
# directly. When a <tt>:scope</tt> is passed without a <tt>:url</tt> the
|
|
1309
|
+
# form just submits to the current URL.
|
|
1310
|
+
# * <tt>:method</tt> - The method to use when submitting the form, usually
|
|
1311
|
+
# either "get" or "post". If "patch", "put", "delete", or another verb
|
|
1312
|
+
# is used, a hidden input named <tt>_method</tt> is added to
|
|
1313
|
+
# simulate the verb over post.
|
|
1314
|
+
# * <tt>:format</tt> - The format of the route the form submits to.
|
|
1315
|
+
# Useful when submitting to another resource type, like <tt>:json</tt>.
|
|
1316
|
+
# Skipped if a <tt>:url</tt> is passed.
|
|
1317
|
+
# * <tt>:scope</tt> - The scope to prefix input field names with and
|
|
1318
|
+
# thereby how the submitted parameters are grouped in controllers.
|
|
1319
|
+
# * <tt>:namespace</tt> - A namespace for your form to ensure uniqueness of
|
|
1320
|
+
# id attributes on form elements. The namespace attribute will be prefixed
|
|
1321
|
+
# with underscore on the generated HTML id.
|
|
1322
|
+
# * <tt>:model</tt> - A model object to infer the <tt>:url</tt> and
|
|
1323
|
+
# <tt>:scope</tt> by, plus fill out input field values.
|
|
1324
|
+
# So if a +title+ attribute is set to "Ahoy!" then a +title+ input
|
|
1325
|
+
# field's value would be "Ahoy!".
|
|
1326
|
+
# If the model is a new record a create form is generated, if an
|
|
1327
|
+
# existing record, however, an update form is generated.
|
|
1328
|
+
# Pass <tt>:scope</tt> or <tt>:url</tt> to override the defaults.
|
|
1329
|
+
# E.g. turn <tt>params[:post]</tt> into <tt>params[:article]</tt>.
|
|
1330
|
+
# * <tt>:authenticity_token</tt> - Authenticity token to use in the form.
|
|
1331
|
+
# Override with a custom authenticity token or pass <tt>false</tt> to
|
|
1332
|
+
# skip the authenticity token field altogether.
|
|
1333
|
+
# Useful when submitting to an external resource like a payment gateway
|
|
1334
|
+
# that might limit the valid fields.
|
|
1335
|
+
# Remote forms may omit the embedded authenticity token by setting
|
|
1336
|
+
# <tt>config.action_view.embed_authenticity_token_in_remote_forms = false</tt>.
|
|
1337
|
+
# This is helpful when fragment-caching the form. Remote forms
|
|
1338
|
+
# get the authenticity token from the <tt>meta</tt> tag, so embedding is
|
|
1339
|
+
# unnecessary unless you support browsers without JavaScript.
|
|
1340
|
+
# * <tt>:local</tt> - By default form submits are remote and unobtrusive XHRs.
|
|
1341
|
+
# Disable remote submits with <tt>local: true</tt>.
|
|
1342
|
+
# * <tt>:skip_enforcing_utf8</tt> - If set to true, a hidden input with name
|
|
1343
|
+
# utf8 is not output.
|
|
1344
|
+
# * <tt>:builder</tt> - Override the object used to build the form.
|
|
1345
|
+
# * <tt>:id</tt> - Optional HTML id attribute.
|
|
1346
|
+
# * <tt>:class</tt> - Optional HTML class attribute.
|
|
1347
|
+
# * <tt>:data</tt> - Optional HTML data attributes.
|
|
1348
|
+
# * <tt>:html</tt> - Other optional HTML attributes for the form tag.
|
|
1349
|
+
#
|
|
1350
|
+
# === Examples
|
|
1351
|
+
#
|
|
1352
|
+
# When not passing a block, +form_with+ just generates an opening form tag.
|
|
1353
|
+
#
|
|
1354
|
+
# <%= form_with(model: @post, url: super_posts_path) %>
|
|
1355
|
+
# <%= form_with(model: @post, scope: :article) %>
|
|
1356
|
+
# <%= form_with(model: @post, format: :json) %>
|
|
1357
|
+
# <%= form_with(model: @post, authenticity_token: false) %> # Disables the token.
|
|
1358
|
+
#
|
|
1359
|
+
# For namespaced routes, like +admin_post_url+:
|
|
1360
|
+
#
|
|
1361
|
+
# <%= form_with(model: [ :admin, @post ]) do |form| %>
|
|
1362
|
+
# ...
|
|
1363
|
+
# <% end %>
|
|
1364
|
+
#
|
|
1365
|
+
# If your resource has associations defined, for example, you want to add comments
|
|
1366
|
+
# to the document given that the routes are set correctly:
|
|
1367
|
+
#
|
|
1368
|
+
# <%= form_with(model: [ @document, Comment.new ]) do |form| %>
|
|
1369
|
+
# ...
|
|
1370
|
+
# <% end %>
|
|
1371
|
+
#
|
|
1372
|
+
# Where <tt>@document = Document.find(params[:id])</tt>.
|
|
1373
|
+
#
|
|
1374
|
+
# === Mixing with other form helpers
|
|
1375
|
+
#
|
|
1376
|
+
# While +form_with+ uses a FormBuilder object it's possible to mix and
|
|
1377
|
+
# match the stand-alone FormHelper methods and methods
|
|
1378
|
+
# from FormTagHelper:
|
|
1379
|
+
#
|
|
1380
|
+
# <%= form_with scope: :person do |form| %>
|
|
1381
|
+
# <%= form.text_field :first_name %>
|
|
1382
|
+
# <%= form.text_field :last_name %>
|
|
1383
|
+
#
|
|
1384
|
+
# <%= text_area :person, :biography %>
|
|
1385
|
+
# <%= check_box_tag "person[admin]", "1", @person.company.admin? %>
|
|
1386
|
+
#
|
|
1387
|
+
# <%= form.submit %>
|
|
1388
|
+
# <% end %>
|
|
1389
|
+
#
|
|
1390
|
+
# Same goes for the methods in FormOptionsHelper and DateHelper designed
|
|
1391
|
+
# to work with an object as a base, like
|
|
1392
|
+
# FormOptionsHelper#collection_select and DateHelper#datetime_select.
|
|
1393
|
+
#
|
|
1394
|
+
# === Setting the method
|
|
1395
|
+
#
|
|
1396
|
+
# You can force the form to use the full array of HTTP verbs by setting
|
|
1397
|
+
#
|
|
1398
|
+
# method: (:get|:post|:patch|:put|:delete)
|
|
1399
|
+
#
|
|
1400
|
+
# in the options hash. If the verb is not GET or POST, which are natively
|
|
1401
|
+
# supported by HTML forms, the form will be set to POST and a hidden input
|
|
1402
|
+
# called _method will carry the intended verb for the server to interpret.
|
|
1403
|
+
#
|
|
1404
|
+
# === Setting HTML options
|
|
1405
|
+
#
|
|
1406
|
+
# You can set data attributes directly in a data hash, but HTML options
|
|
1407
|
+
# besides id and class must be wrapped in an HTML key:
|
|
1408
|
+
#
|
|
1409
|
+
# <%= form_with(model: @post, data: { behavior: "autosave" }, html: { name: "go" }) do |form| %>
|
|
1410
|
+
# ...
|
|
1411
|
+
# <% end %>
|
|
1412
|
+
#
|
|
1413
|
+
# generates
|
|
1414
|
+
#
|
|
1415
|
+
# <form action="/posts/123" method="post" data-behavior="autosave" name="go">
|
|
1416
|
+
# <input name="_method" type="hidden" value="patch" />
|
|
1417
|
+
# ...
|
|
1418
|
+
# </form>
|
|
1419
|
+
#
|
|
1420
|
+
# === Removing hidden model id's
|
|
1421
|
+
#
|
|
1422
|
+
# The +form_with+ method automatically includes the model id as a hidden field in the form.
|
|
1423
|
+
# This is used to maintain the correlation between the form data and its associated model.
|
|
1424
|
+
# Some ORM systems do not use IDs on nested models so in this case you want to be able
|
|
1425
|
+
# to disable the hidden id.
|
|
1426
|
+
#
|
|
1427
|
+
# In the following example the Post model has many Comments stored within it in a NoSQL database,
|
|
1428
|
+
# thus there is no primary key for comments.
|
|
1429
|
+
#
|
|
1430
|
+
# <%= form_with(model: @post) do |form| %>
|
|
1431
|
+
# <%= form.fields(:comments, skip_id: true) do |fields| %>
|
|
1432
|
+
# ...
|
|
1433
|
+
# <% end %>
|
|
1434
|
+
# <% end %>
|
|
1435
|
+
#
|
|
1436
|
+
# === Customized form builders
|
|
1437
|
+
#
|
|
1438
|
+
# You can also build forms using a customized FormBuilder class. Subclass
|
|
1439
|
+
# FormBuilder and override or define some more helpers, then use your
|
|
1440
|
+
# custom builder. For example, let's say you made a helper to
|
|
1441
|
+
# automatically add labels to form inputs.
|
|
1442
|
+
#
|
|
1443
|
+
# <%= form_with model: @person, url: { action: "create" }, builder: LabellingFormBuilder do |form| %>
|
|
1444
|
+
# <%= form.text_field :first_name %>
|
|
1445
|
+
# <%= form.text_field :last_name %>
|
|
1446
|
+
# <%= form.text_area :biography %>
|
|
1447
|
+
# <%= form.check_box :admin %>
|
|
1448
|
+
# <%= form.submit %>
|
|
1449
|
+
# <% end %>
|
|
1450
|
+
#
|
|
1451
|
+
# In this case, if you use:
|
|
1452
|
+
#
|
|
1453
|
+
# <%= render form %>
|
|
1454
|
+
#
|
|
1455
|
+
# The rendered template is <tt>people/_labelling_form</tt> and the local
|
|
1456
|
+
# variable referencing the form builder is called
|
|
1457
|
+
# <tt>labelling_form</tt>.
|
|
1458
|
+
#
|
|
1459
|
+
# The custom FormBuilder class is automatically merged with the options
|
|
1460
|
+
# of a nested +fields+ call, unless it's explicitly set.
|
|
1461
|
+
#
|
|
1462
|
+
# In many cases you will want to wrap the above in another helper, so you
|
|
1463
|
+
# could do something like the following:
|
|
1464
|
+
#
|
|
1465
|
+
# def labelled_form_with(**options, &block)
|
|
1466
|
+
# form_with(**options.merge(builder: LabellingFormBuilder), &block)
|
|
1467
|
+
# end
|
|
1468
|
+
def form_with: (?format: untyped? format, ?url: untyped? url, ?scope: untyped? scope, ?model: untyped? model, **untyped options) { (Helpers::FormBuilder) -> String } -> String
|
|
1469
|
+
|
|
1470
|
+
# Returns a label tag tailored for labelling an input field for a specified attribute (identified by +method+) on an object
|
|
1471
|
+
# assigned to the template (identified by +object+). The text of label will default to the attribute name unless a translation
|
|
1472
|
+
# is found in the current I18n locale (through helpers.label.<modelname>.<attribute>) or you specify it explicitly.
|
|
1473
|
+
# Additional options on the label tag can be passed as a hash with +options+. These options will be tagged
|
|
1474
|
+
# onto the HTML as an HTML element attribute as in the example shown, except for the <tt>:value</tt> option, which is designed to
|
|
1475
|
+
# target labels for radio_button tags (where the value is used in the ID of the input tag).
|
|
1476
|
+
#
|
|
1477
|
+
# ==== Examples
|
|
1478
|
+
# label(:post, :title)
|
|
1479
|
+
# # => <label for="post_title">Title</label>
|
|
1480
|
+
#
|
|
1481
|
+
# You can localize your labels based on model and attribute names.
|
|
1482
|
+
# For example you can define the following in your locale (e.g. en.yml)
|
|
1483
|
+
#
|
|
1484
|
+
# helpers:
|
|
1485
|
+
# label:
|
|
1486
|
+
# post:
|
|
1487
|
+
# body: "Write your entire text here"
|
|
1488
|
+
#
|
|
1489
|
+
# Which then will result in
|
|
1490
|
+
#
|
|
1491
|
+
# label(:post, :body)
|
|
1492
|
+
# # => <label for="post_body">Write your entire text here</label>
|
|
1493
|
+
#
|
|
1494
|
+
# Localization can also be based purely on the translation of the attribute-name
|
|
1495
|
+
# (if you are using ActiveRecord):
|
|
1496
|
+
#
|
|
1497
|
+
# activerecord:
|
|
1498
|
+
# attributes:
|
|
1499
|
+
# post:
|
|
1500
|
+
# cost: "Total cost"
|
|
1501
|
+
#
|
|
1502
|
+
# label(:post, :cost)
|
|
1503
|
+
# # => <label for="post_cost">Total cost</label>
|
|
1504
|
+
#
|
|
1505
|
+
# label(:post, :title, "A short title")
|
|
1506
|
+
# # => <label for="post_title">A short title</label>
|
|
1507
|
+
#
|
|
1508
|
+
# label(:post, :title, "A short title", class: "title_label")
|
|
1509
|
+
# # => <label for="post_title" class="title_label">A short title</label>
|
|
1510
|
+
#
|
|
1511
|
+
# label(:post, :privacy, "Public Post", value: "public")
|
|
1512
|
+
# # => <label for="post_privacy_public">Public Post</label>
|
|
1513
|
+
#
|
|
1514
|
+
# label(:post, :terms) do
|
|
1515
|
+
# raw('Accept <a href="/terms">Terms</a>.')
|
|
1516
|
+
# end
|
|
1517
|
+
# # => <label for="post_terms">Accept <a href="/terms">Terms</a>.</label>
|
|
1518
|
+
def label: (untyped object_name, untyped method, ?untyped? content_or_options, ?untyped? options) ?{ () -> untyped } -> untyped
|
|
1519
|
+
end
|
|
1520
|
+
end
|
|
1521
|
+
end
|
|
1522
|
+
|
|
1523
|
+
module ActionView
|
|
1524
|
+
module Helpers
|
|
1525
|
+
module RenderingHelper
|
|
1526
|
+
# Returns the result of a render that's dictated by the options hash. The primary options are:
|
|
1527
|
+
#
|
|
1528
|
+
# * <tt>:partial</tt> - See <tt>ActionView::PartialRenderer</tt>.
|
|
1529
|
+
# * <tt>:file</tt> - Renders an explicit template file (this used to be the old default), add :locals to pass in those.
|
|
1530
|
+
# * <tt>:inline</tt> - Renders an inline template similar to how it's done in the controller.
|
|
1531
|
+
# * <tt>:plain</tt> - Renders the text passed in out. Setting the content
|
|
1532
|
+
# type as <tt>text/plain</tt>.
|
|
1533
|
+
# * <tt>:html</tt> - Renders the HTML safe string passed in out, otherwise
|
|
1534
|
+
# performs HTML escape on the string first. Setting the content type as
|
|
1535
|
+
# <tt>text/html</tt>.
|
|
1536
|
+
# * <tt>:body</tt> - Renders the text passed in, and inherits the content
|
|
1537
|
+
# type of <tt>text/plain</tt> from <tt>ActionDispatch::Response</tt>
|
|
1538
|
+
# object.
|
|
1539
|
+
#
|
|
1540
|
+
# If no options hash is passed or :update specified, the default is to render a partial and use the second parameter
|
|
1541
|
+
# as the locals hash.
|
|
1542
|
+
def render: (?(String | Symbol) | ::Hash[untyped, untyped] options, ?::Hash[untyped, untyped] locals) ?{ () -> untyped } -> String
|
|
1543
|
+
end
|
|
1544
|
+
end
|
|
1545
|
+
end
|
|
1546
|
+
|
|
1547
|
+
module ActionView
|
|
1548
|
+
module Helpers
|
|
1549
|
+
module TagHelper
|
|
1550
|
+
# Returns an HTML block tag of type +name+ surrounding the +content+. Add
|
|
1551
|
+
# HTML attributes by passing an attributes hash to +options+.
|
|
1552
|
+
# Instead of passing the content as an argument, you can also use a block
|
|
1553
|
+
# in which case, you pass your +options+ as the second parameter.
|
|
1554
|
+
# Set escape to false to disable attribute value escaping.
|
|
1555
|
+
# Note: this is legacy syntax, see +tag+ method description for details.
|
|
1556
|
+
#
|
|
1557
|
+
# ==== Options
|
|
1558
|
+
# The +options+ hash can be used with attributes with no value like (<tt>disabled</tt> and
|
|
1559
|
+
# <tt>readonly</tt>), which you can give a value of true in the +options+ hash. You can use
|
|
1560
|
+
# symbols or strings for the attribute names.
|
|
1561
|
+
#
|
|
1562
|
+
# ==== Examples
|
|
1563
|
+
# content_tag(:p, "Hello world!")
|
|
1564
|
+
# # => <p>Hello world!</p>
|
|
1565
|
+
# content_tag(:div, content_tag(:p, "Hello world!"), class: "strong")
|
|
1566
|
+
# # => <div class="strong"><p>Hello world!</p></div>
|
|
1567
|
+
# content_tag(:div, "Hello world!", class: ["strong", "highlight"])
|
|
1568
|
+
# # => <div class="strong highlight">Hello world!</div>
|
|
1569
|
+
# content_tag("select", options, multiple: true)
|
|
1570
|
+
# # => <select multiple="multiple">...options...</select>
|
|
1571
|
+
#
|
|
1572
|
+
# <%= content_tag :div, class: "strong" do -%>
|
|
1573
|
+
# Hello world!
|
|
1574
|
+
# <% end -%>
|
|
1575
|
+
# # => <div class="strong">Hello world!</div>
|
|
1576
|
+
def content_tag: (untyped name, ?untyped? content_or_options_with_block, ?untyped? options, ?bool escape) ?{ () -> untyped } -> untyped
|
|
1577
|
+
end
|
|
1578
|
+
end
|
|
1579
|
+
end
|
|
1580
|
+
|
|
1581
|
+
module ActionView
|
|
1582
|
+
module Helpers
|
|
1583
|
+
module UrlHelper
|
|
1584
|
+
# Creates an anchor element of the given +name+ using a URL created by the set of +options+.
|
|
1585
|
+
# See the valid options in the documentation for +url_for+. It's also possible to
|
|
1586
|
+
# pass a String instead of an options hash, which generates an anchor element that uses the
|
|
1587
|
+
# value of the String as the href for the link. Using a <tt>:back</tt> Symbol instead
|
|
1588
|
+
# of an options hash will generate a link to the referrer (a JavaScript back link
|
|
1589
|
+
# will be used in place of a referrer if none exists). If +nil+ is passed as the name
|
|
1590
|
+
# the value of the link itself will become the name.
|
|
1591
|
+
#
|
|
1592
|
+
# ==== Signatures
|
|
1593
|
+
#
|
|
1594
|
+
# link_to(body, url, html_options = {})
|
|
1595
|
+
# # url is a String; you can use URL helpers like
|
|
1596
|
+
# # posts_path
|
|
1597
|
+
#
|
|
1598
|
+
# link_to(body, url_options = {}, html_options = {})
|
|
1599
|
+
# # url_options, except :method, is passed to url_for
|
|
1600
|
+
#
|
|
1601
|
+
# link_to(options = {}, html_options = {}) do
|
|
1602
|
+
# # name
|
|
1603
|
+
# end
|
|
1604
|
+
#
|
|
1605
|
+
# link_to(url, html_options = {}) do
|
|
1606
|
+
# # name
|
|
1607
|
+
# end
|
|
1608
|
+
#
|
|
1609
|
+
# ==== Options
|
|
1610
|
+
# * <tt>:data</tt> - This option can be used to add custom data attributes.
|
|
1611
|
+
# * <tt>method: symbol of HTTP verb</tt> - This modifier will dynamically
|
|
1612
|
+
# create an HTML form and immediately submit the form for processing using
|
|
1613
|
+
# the HTTP verb specified. Useful for having links perform a POST operation
|
|
1614
|
+
# in dangerous actions like deleting a record (which search bots can follow
|
|
1615
|
+
# while spidering your site). Supported verbs are <tt>:post</tt>, <tt>:delete</tt>, <tt>:patch</tt>, and <tt>:put</tt>.
|
|
1616
|
+
# Note that if the user has JavaScript disabled, the request will fall back
|
|
1617
|
+
# to using GET. If <tt>href: '#'</tt> is used and the user has JavaScript
|
|
1618
|
+
# disabled clicking the link will have no effect. If you are relying on the
|
|
1619
|
+
# POST behavior, you should check for it in your controller's action by using
|
|
1620
|
+
# the request object's methods for <tt>post?</tt>, <tt>delete?</tt>, <tt>patch?</tt>, or <tt>put?</tt>.
|
|
1621
|
+
# * <tt>remote: true</tt> - This will allow the unobtrusive JavaScript
|
|
1622
|
+
# driver to make an Ajax request to the URL in question instead of following
|
|
1623
|
+
# the link. The drivers each provide mechanisms for listening for the
|
|
1624
|
+
# completion of the Ajax request and performing JavaScript operations once
|
|
1625
|
+
# they're complete
|
|
1626
|
+
#
|
|
1627
|
+
# ==== Data attributes
|
|
1628
|
+
#
|
|
1629
|
+
# * <tt>confirm: 'question?'</tt> - This will allow the unobtrusive JavaScript
|
|
1630
|
+
# driver to prompt with the question specified (in this case, the
|
|
1631
|
+
# resulting text would be <tt>question?</tt>. If the user accepts, the
|
|
1632
|
+
# link is processed normally, otherwise no action is taken.
|
|
1633
|
+
# * <tt>:disable_with</tt> - Value of this parameter will be used as the
|
|
1634
|
+
# name for a disabled version of the link. This feature is provided by
|
|
1635
|
+
# the unobtrusive JavaScript driver.
|
|
1636
|
+
#
|
|
1637
|
+
# ==== Examples
|
|
1638
|
+
# Because it relies on +url_for+, +link_to+ supports both older-style controller/action/id arguments
|
|
1639
|
+
# and newer RESTful routes. Current Rails style favors RESTful routes whenever possible, so base
|
|
1640
|
+
# your application on resources and use
|
|
1641
|
+
#
|
|
1642
|
+
# link_to "Profile", profile_path(@profile)
|
|
1643
|
+
# # => <a href="/profiles/1">Profile</a>
|
|
1644
|
+
#
|
|
1645
|
+
# or the even pithier
|
|
1646
|
+
#
|
|
1647
|
+
# link_to "Profile", @profile
|
|
1648
|
+
# # => <a href="/profiles/1">Profile</a>
|
|
1649
|
+
#
|
|
1650
|
+
# in place of the older more verbose, non-resource-oriented
|
|
1651
|
+
#
|
|
1652
|
+
# link_to "Profile", controller: "profiles", action: "show", id: @profile
|
|
1653
|
+
# # => <a href="/profiles/show/1">Profile</a>
|
|
1654
|
+
#
|
|
1655
|
+
# Similarly,
|
|
1656
|
+
#
|
|
1657
|
+
# link_to "Profiles", profiles_path
|
|
1658
|
+
# # => <a href="/profiles">Profiles</a>
|
|
1659
|
+
#
|
|
1660
|
+
# is better than
|
|
1661
|
+
#
|
|
1662
|
+
# link_to "Profiles", controller: "profiles"
|
|
1663
|
+
# # => <a href="/profiles">Profiles</a>
|
|
1664
|
+
#
|
|
1665
|
+
# When name is +nil+ the href is presented instead
|
|
1666
|
+
#
|
|
1667
|
+
# link_to nil, "http://example.com"
|
|
1668
|
+
# # => <a href="http://www.example.com">http://www.example.com</a>
|
|
1669
|
+
#
|
|
1670
|
+
# You can use a block as well if your link target is hard to fit into the name parameter. ERB example:
|
|
1671
|
+
#
|
|
1672
|
+
# <%= link_to(@profile) do %>
|
|
1673
|
+
# <strong><%= @profile.name %></strong> -- <span>Check it out!</span>
|
|
1674
|
+
# <% end %>
|
|
1675
|
+
# # => <a href="/profiles/1">
|
|
1676
|
+
# <strong>David</strong> -- <span>Check it out!</span>
|
|
1677
|
+
# </a>
|
|
1678
|
+
#
|
|
1679
|
+
# Classes and ids for CSS are easy to produce:
|
|
1680
|
+
#
|
|
1681
|
+
# link_to "Articles", articles_path, id: "news", class: "article"
|
|
1682
|
+
# # => <a href="/articles" class="article" id="news">Articles</a>
|
|
1683
|
+
#
|
|
1684
|
+
# Be careful when using the older argument style, as an extra literal hash is needed:
|
|
1685
|
+
#
|
|
1686
|
+
# link_to "Articles", { controller: "articles" }, id: "news", class: "article"
|
|
1687
|
+
# # => <a href="/articles" class="article" id="news">Articles</a>
|
|
1688
|
+
#
|
|
1689
|
+
# Leaving the hash off gives the wrong link:
|
|
1690
|
+
#
|
|
1691
|
+
# link_to "WRONG!", controller: "articles", id: "news", class: "article"
|
|
1692
|
+
# # => <a href="/articles/index/news?class=article">WRONG!</a>
|
|
1693
|
+
#
|
|
1694
|
+
# +link_to+ can also produce links with anchors or query strings:
|
|
1695
|
+
#
|
|
1696
|
+
# link_to "Comment wall", profile_path(@profile, anchor: "wall")
|
|
1697
|
+
# # => <a href="/profiles/1#wall">Comment wall</a>
|
|
1698
|
+
#
|
|
1699
|
+
# link_to "Ruby on Rails search", controller: "searches", query: "ruby on rails"
|
|
1700
|
+
# # => <a href="/searches?query=ruby+on+rails">Ruby on Rails search</a>
|
|
1701
|
+
#
|
|
1702
|
+
# link_to "Nonsense search", searches_path(foo: "bar", baz: "quux")
|
|
1703
|
+
# # => <a href="/searches?foo=bar&baz=quux">Nonsense search</a>
|
|
1704
|
+
#
|
|
1705
|
+
# The only option specific to +link_to+ (<tt>:method</tt>) is used as follows:
|
|
1706
|
+
#
|
|
1707
|
+
# link_to("Destroy", "http://www.example.com", method: :delete)
|
|
1708
|
+
# # => <a href='http://www.example.com' rel="nofollow" data-method="delete">Destroy</a>
|
|
1709
|
+
#
|
|
1710
|
+
# You can also use custom data attributes using the <tt>:data</tt> option:
|
|
1711
|
+
#
|
|
1712
|
+
# link_to "Visit Other Site", "http://www.rubyonrails.org/", data: { confirm: "Are you sure?" }
|
|
1713
|
+
# # => <a href="http://www.rubyonrails.org/" data-confirm="Are you sure?">Visit Other Site</a>
|
|
1714
|
+
#
|
|
1715
|
+
# Also you can set any link attributes such as <tt>target</tt>, <tt>rel</tt>, <tt>type</tt>:
|
|
1716
|
+
#
|
|
1717
|
+
# link_to "External link", "http://www.rubyonrails.org/", target: "_blank", rel: "nofollow"
|
|
1718
|
+
# # => <a href="http://www.rubyonrails.org/" target="_blank" rel="nofollow">External link</a>
|
|
1719
|
+
def link_to: (?untyped? name, ?untyped? options, ?untyped? html_options) ?{ () -> untyped } -> untyped
|
|
1720
|
+
|
|
1721
|
+
# Generates a form containing a single button that submits to the URL created
|
|
1722
|
+
# by the set of +options+. This is the safest method to ensure links that
|
|
1723
|
+
# cause changes to your data are not triggered by search bots or accelerators.
|
|
1724
|
+
# If the HTML button does not work with your layout, you can also consider
|
|
1725
|
+
# using the +link_to+ method with the <tt>:method</tt> modifier as described in
|
|
1726
|
+
# the +link_to+ documentation.
|
|
1727
|
+
#
|
|
1728
|
+
# By default, the generated form element has a class name of <tt>button_to</tt>
|
|
1729
|
+
# to allow styling of the form itself and its children. This can be changed
|
|
1730
|
+
# using the <tt>:form_class</tt> modifier within +html_options+. You can control
|
|
1731
|
+
# the form submission and input element behavior using +html_options+.
|
|
1732
|
+
# This method accepts the <tt>:method</tt> modifier described in the +link_to+ documentation.
|
|
1733
|
+
# If no <tt>:method</tt> modifier is given, it will default to performing a POST operation.
|
|
1734
|
+
# You can also disable the button by passing <tt>disabled: true</tt> in +html_options+.
|
|
1735
|
+
# If you are using RESTful routes, you can pass the <tt>:method</tt>
|
|
1736
|
+
# to change the HTTP verb used to submit the form.
|
|
1737
|
+
#
|
|
1738
|
+
# ==== Options
|
|
1739
|
+
# The +options+ hash accepts the same options as +url_for+.
|
|
1740
|
+
#
|
|
1741
|
+
# There are a few special +html_options+:
|
|
1742
|
+
# * <tt>:method</tt> - Symbol of HTTP verb. Supported verbs are <tt>:post</tt>, <tt>:get</tt>,
|
|
1743
|
+
# <tt>:delete</tt>, <tt>:patch</tt>, and <tt>:put</tt>. By default it will be <tt>:post</tt>.
|
|
1744
|
+
# * <tt>:disabled</tt> - If set to true, it will generate a disabled button.
|
|
1745
|
+
# * <tt>:data</tt> - This option can be used to add custom data attributes.
|
|
1746
|
+
# * <tt>:remote</tt> - If set to true, will allow the Unobtrusive JavaScript drivers to control the
|
|
1747
|
+
# submit behavior. By default this behavior is an ajax submit.
|
|
1748
|
+
# * <tt>:form</tt> - This hash will be form attributes
|
|
1749
|
+
# * <tt>:form_class</tt> - This controls the class of the form within which the submit button will
|
|
1750
|
+
# be placed
|
|
1751
|
+
# * <tt>:params</tt> - Hash of parameters to be rendered as hidden fields within the form.
|
|
1752
|
+
#
|
|
1753
|
+
# ==== Data attributes
|
|
1754
|
+
#
|
|
1755
|
+
# * <tt>:confirm</tt> - This will use the unobtrusive JavaScript driver to
|
|
1756
|
+
# prompt with the question specified. If the user accepts, the link is
|
|
1757
|
+
# processed normally, otherwise no action is taken.
|
|
1758
|
+
# * <tt>:disable_with</tt> - Value of this parameter will be
|
|
1759
|
+
# used as the value for a disabled version of the submit
|
|
1760
|
+
# button when the form is submitted. This feature is provided
|
|
1761
|
+
# by the unobtrusive JavaScript driver.
|
|
1762
|
+
#
|
|
1763
|
+
# ==== Examples
|
|
1764
|
+
# <%= button_to "New", action: "new" %>
|
|
1765
|
+
# # => "<form method="post" action="/controller/new" class="button_to">
|
|
1766
|
+
# # <input value="New" type="submit" />
|
|
1767
|
+
# # </form>"
|
|
1768
|
+
#
|
|
1769
|
+
# <%= button_to "New", new_article_path %>
|
|
1770
|
+
# # => "<form method="post" action="/articles/new" class="button_to">
|
|
1771
|
+
# # <input value="New" type="submit" />
|
|
1772
|
+
# # </form>"
|
|
1773
|
+
#
|
|
1774
|
+
# <%= button_to [:make_happy, @user] do %>
|
|
1775
|
+
# Make happy <strong><%= @user.name %></strong>
|
|
1776
|
+
# <% end %>
|
|
1777
|
+
# # => "<form method="post" action="/users/1/make_happy" class="button_to">
|
|
1778
|
+
# # <button type="submit">
|
|
1779
|
+
# # Make happy <strong><%= @user.name %></strong>
|
|
1780
|
+
# # </button>
|
|
1781
|
+
# # </form>"
|
|
1782
|
+
#
|
|
1783
|
+
# <%= button_to "New", { action: "new" }, form_class: "new-thing" %>
|
|
1784
|
+
# # => "<form method="post" action="/controller/new" class="new-thing">
|
|
1785
|
+
# # <input value="New" type="submit" />
|
|
1786
|
+
# # </form>"
|
|
1787
|
+
#
|
|
1788
|
+
#
|
|
1789
|
+
# <%= button_to "Create", { action: "create" }, remote: true, form: { "data-type" => "json" } %>
|
|
1790
|
+
# # => "<form method="post" action="/images/create" class="button_to" data-remote="true" data-type="json">
|
|
1791
|
+
# # <input value="Create" type="submit" />
|
|
1792
|
+
# # <input name="authenticity_token" type="hidden" value="10f2163b45388899ad4d5ae948988266befcb6c3d1b2451cf657a0c293d605a6"/>
|
|
1793
|
+
# # </form>"
|
|
1794
|
+
#
|
|
1795
|
+
#
|
|
1796
|
+
# <%= button_to "Delete Image", { action: "delete", id: @image.id },
|
|
1797
|
+
# method: :delete, data: { confirm: "Are you sure?" } %>
|
|
1798
|
+
# # => "<form method="post" action="/images/delete/1" class="button_to">
|
|
1799
|
+
# # <input type="hidden" name="_method" value="delete" />
|
|
1800
|
+
# # <input data-confirm='Are you sure?' value="Delete Image" type="submit" />
|
|
1801
|
+
# # <input name="authenticity_token" type="hidden" value="10f2163b45388899ad4d5ae948988266befcb6c3d1b2451cf657a0c293d605a6"/>
|
|
1802
|
+
# # </form>"
|
|
1803
|
+
#
|
|
1804
|
+
#
|
|
1805
|
+
# <%= button_to('Destroy', 'http://www.example.com',
|
|
1806
|
+
# method: "delete", remote: true, data: { confirm: 'Are you sure?', disable_with: 'loading...' }) %>
|
|
1807
|
+
# # => "<form class='button_to' method='post' action='http://www.example.com' data-remote='true'>
|
|
1808
|
+
# # <input name='_method' value='delete' type='hidden' />
|
|
1809
|
+
# # <input value='Destroy' type='submit' data-disable-with='loading...' data-confirm='Are you sure?' />
|
|
1810
|
+
# # <input name="authenticity_token" type="hidden" value="10f2163b45388899ad4d5ae948988266befcb6c3d1b2451cf657a0c293d605a6"/>
|
|
1811
|
+
# # </form>"
|
|
1812
|
+
# #
|
|
1813
|
+
def button_to: (?untyped? name, ?untyped? options, ?untyped? html_options) ?{ () -> untyped } -> untyped
|
|
1814
|
+
|
|
1815
|
+
# Creates a link tag of the given +name+ using a URL created by the set of
|
|
1816
|
+
# +options+ unless the current request URI is the same as the links, in
|
|
1817
|
+
# which case only the name is returned (or the given block is yielded, if
|
|
1818
|
+
# one exists). You can give +link_to_unless_current+ a block which will
|
|
1819
|
+
# specialize the default behavior (e.g., show a "Start Here" link rather
|
|
1820
|
+
# than the link's text).
|
|
1821
|
+
#
|
|
1822
|
+
# ==== Examples
|
|
1823
|
+
# Let's say you have a navigation menu...
|
|
1824
|
+
#
|
|
1825
|
+
# <ul id="navbar">
|
|
1826
|
+
# <li><%= link_to_unless_current("Home", { action: "index" }) %></li>
|
|
1827
|
+
# <li><%= link_to_unless_current("About Us", { action: "about" }) %></li>
|
|
1828
|
+
# </ul>
|
|
1829
|
+
#
|
|
1830
|
+
# If in the "about" action, it will render...
|
|
1831
|
+
#
|
|
1832
|
+
# <ul id="navbar">
|
|
1833
|
+
# <li><a href="/controller/index">Home</a></li>
|
|
1834
|
+
# <li>About Us</li>
|
|
1835
|
+
# </ul>
|
|
1836
|
+
#
|
|
1837
|
+
# ...but if in the "index" action, it will render:
|
|
1838
|
+
#
|
|
1839
|
+
# <ul id="navbar">
|
|
1840
|
+
# <li>Home</li>
|
|
1841
|
+
# <li><a href="/controller/about">About Us</a></li>
|
|
1842
|
+
# </ul>
|
|
1843
|
+
#
|
|
1844
|
+
# The implicit block given to +link_to_unless_current+ is evaluated if the current
|
|
1845
|
+
# action is the action given. So, if we had a comments page and wanted to render a
|
|
1846
|
+
# "Go Back" link instead of a link to the comments page, we could do something like this...
|
|
1847
|
+
#
|
|
1848
|
+
# <%=
|
|
1849
|
+
# link_to_unless_current("Comment", { controller: "comments", action: "new" }) do
|
|
1850
|
+
# link_to("Go back", { controller: "posts", action: "index" })
|
|
1851
|
+
# end
|
|
1852
|
+
# %>
|
|
1853
|
+
def link_to_unless_current: (untyped name, ?::Hash[untyped, untyped] options, ?::Hash[untyped, untyped] html_options) ?{ () -> untyped } -> untyped
|
|
1854
|
+
|
|
1855
|
+
# Creates a link tag of the given +name+ using a URL created by the set of
|
|
1856
|
+
# +options+ unless +condition+ is true, in which case only the name is
|
|
1857
|
+
# returned. To specialize the default behavior (i.e., show a login link rather
|
|
1858
|
+
# than just the plaintext link text), you can pass a block that
|
|
1859
|
+
# accepts the name or the full argument list for +link_to_unless+.
|
|
1860
|
+
#
|
|
1861
|
+
# ==== Examples
|
|
1862
|
+
# <%= link_to_unless(@current_user.nil?, "Reply", { action: "reply" }) %>
|
|
1863
|
+
# # If the user is logged in...
|
|
1864
|
+
# # => <a href="/controller/reply/">Reply</a>
|
|
1865
|
+
#
|
|
1866
|
+
# <%=
|
|
1867
|
+
# link_to_unless(@current_user.nil?, "Reply", { action: "reply" }) do |name|
|
|
1868
|
+
# link_to(name, { controller: "accounts", action: "signup" })
|
|
1869
|
+
# end
|
|
1870
|
+
# %>
|
|
1871
|
+
# # If the user is logged in...
|
|
1872
|
+
# # => <a href="/controller/reply/">Reply</a>
|
|
1873
|
+
# # If not...
|
|
1874
|
+
# # => <a href="/accounts/signup">Reply</a>
|
|
1875
|
+
def link_to_unless: (untyped condition, untyped name, ?::Hash[untyped, untyped] options, ?::Hash[untyped, untyped] html_options) ?{ () -> untyped } -> untyped
|
|
1876
|
+
|
|
1877
|
+
# Creates a link tag of the given +name+ using a URL created by the set of
|
|
1878
|
+
# +options+ if +condition+ is true, otherwise only the name is
|
|
1879
|
+
# returned. To specialize the default behavior, you can pass a block that
|
|
1880
|
+
# accepts the name or the full argument list for +link_to_unless+ (see the examples
|
|
1881
|
+
# in +link_to_unless+).
|
|
1882
|
+
#
|
|
1883
|
+
# ==== Examples
|
|
1884
|
+
# <%= link_to_if(@current_user.nil?, "Login", { controller: "sessions", action: "new" }) %>
|
|
1885
|
+
# # If the user isn't logged in...
|
|
1886
|
+
# # => <a href="/sessions/new/">Login</a>
|
|
1887
|
+
#
|
|
1888
|
+
# <%=
|
|
1889
|
+
# link_to_if(@current_user.nil?, "Login", { controller: "sessions", action: "new" }) do
|
|
1890
|
+
# link_to(@current_user.login, { controller: "accounts", action: "show", id: @current_user })
|
|
1891
|
+
# end
|
|
1892
|
+
# %>
|
|
1893
|
+
# # If the user isn't logged in...
|
|
1894
|
+
# # => <a href="/sessions/new/">Login</a>
|
|
1895
|
+
# # If they are logged in...
|
|
1896
|
+
# # => <a href="/accounts/show/3">my_username</a>
|
|
1897
|
+
def link_to_if: (untyped condition, untyped name, ?::Hash[untyped, untyped] options, ?::Hash[untyped, untyped] html_options) ?{ () -> untyped } -> untyped
|
|
1898
|
+
|
|
1899
|
+
# Creates a mailto link tag to the specified +email_address+, which is
|
|
1900
|
+
# also used as the name of the link unless +name+ is specified. Additional
|
|
1901
|
+
# HTML attributes for the link can be passed in +html_options+.
|
|
1902
|
+
#
|
|
1903
|
+
# +mail_to+ has several methods for customizing the email itself by
|
|
1904
|
+
# passing special keys to +html_options+.
|
|
1905
|
+
#
|
|
1906
|
+
# ==== Options
|
|
1907
|
+
# * <tt>:subject</tt> - Preset the subject line of the email.
|
|
1908
|
+
# * <tt>:body</tt> - Preset the body of the email.
|
|
1909
|
+
# * <tt>:cc</tt> - Carbon Copy additional recipients on the email.
|
|
1910
|
+
# * <tt>:bcc</tt> - Blind Carbon Copy additional recipients on the email.
|
|
1911
|
+
# * <tt>:reply_to</tt> - Preset the Reply-To field of the email.
|
|
1912
|
+
#
|
|
1913
|
+
# ==== Obfuscation
|
|
1914
|
+
# Prior to Rails 4.0, +mail_to+ provided options for encoding the address
|
|
1915
|
+
# in order to hinder email harvesters. To take advantage of these options,
|
|
1916
|
+
# install the +actionview-encoded_mail_to+ gem.
|
|
1917
|
+
#
|
|
1918
|
+
# ==== Examples
|
|
1919
|
+
# mail_to "me@domain.com"
|
|
1920
|
+
# # => <a href="mailto:me@domain.com">me@domain.com</a>
|
|
1921
|
+
#
|
|
1922
|
+
# mail_to "me@domain.com", "My email"
|
|
1923
|
+
# # => <a href="mailto:me@domain.com">My email</a>
|
|
1924
|
+
#
|
|
1925
|
+
# mail_to "me@domain.com", "My email", cc: "ccaddress@domain.com",
|
|
1926
|
+
# subject: "This is an example email"
|
|
1927
|
+
# # => <a href="mailto:me@domain.com?cc=ccaddress@domain.com&subject=This%20is%20an%20example%20email">My email</a>
|
|
1928
|
+
#
|
|
1929
|
+
# You can use a block as well if your link target is hard to fit into the name parameter. ERB example:
|
|
1930
|
+
#
|
|
1931
|
+
# <%= mail_to "me@domain.com" do %>
|
|
1932
|
+
# <strong>Email me:</strong> <span>me@domain.com</span>
|
|
1933
|
+
# <% end %>
|
|
1934
|
+
# # => <a href="mailto:me@domain.com">
|
|
1935
|
+
# <strong>Email me:</strong> <span>me@domain.com</span>
|
|
1936
|
+
# </a>
|
|
1937
|
+
def mail_to: (untyped email_address, ?untyped? name, ?::Hash[untyped, untyped] html_options) ?{ () -> untyped } -> untyped
|
|
1938
|
+
end
|
|
1939
|
+
end
|
|
1940
|
+
end
|