New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Update "Action View Form Helpers" guide #31972
Update "Action View Form Helpers" guide #31972
Conversation
r? @eileencodes (@rails-bot has picked a reviewer for you, use r? to override) |
9a6b9cb
to
21915f2
Compare
@@ -76,10 +78,6 @@ This will generate the following HTML: | |||
|
|||
TIP: For every form input, an ID attribute is generated from its name (`"q"` in above example). These IDs can be very useful for CSS styling or manipulation of form controls with JavaScript. | |||
|
|||
Besides `text_field_tag` and `submit_tag`, there is a similar helper for _every_ form control in HTML. | |||
|
|||
IMPORTANT: Always use "GET" as the method for search forms. This allows users to bookmark a specific search and get back to it. More generally Rails encourages you to use the right HTTP verb for an action. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is this useless?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I just returned this. I removed "Always" from the beginning of the sentence since it sounds like an obligation with this word.
``` | ||
|
||
Upon form submission the value entered by the user will be stored in `params[:person][:name]`. The `params[:person]` hash is suitable for passing to `Person.new` or, if `@person` is an instance of Person, `@person.update`. While the name of an attribute is the most common second parameter to these helpers this is not compulsory. In the example above, as long as person objects have a `name` and a `name=` method Rails will be happy. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is this useless?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We have another place where we completly explain how to use params
, see https://edgeguides.rubyonrails.org/action_controller_overview.html#strong-parameters
guides/source/form_helpers.md
Outdated
The object yielded by `fields_for` is a form builder like the one yielded by `form_for` (in fact `form_for` calls `fields_for` internally). | ||
The object yielded by `fields_for` is a form builder like the one yielded by `form_for`. | ||
|
||
NOTE: There is method [`form_with`](http://api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html#method-i-form_with) that creates a form tag based on mixing URLs, scopes, or models. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what mixing means in this context?
guides/source/form_helpers.md
Outdated
# short-style: | ||
form_for(@article) | ||
``` | ||
|
||
Notice how the short-style `form_for` invocation is conveniently the same, regardless of the record being new or existing. Record identification is smart enough to figure out if the record is new by asking `record.new_record?`. It also selects the correct path to submit to and the name based on the class of the object. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not internal and it is not useless. Where we would explain this behavior to the users?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agree. I just returned this.
guides/source/form_helpers.md
Outdated
``` | ||
|
||
The first argument to `options_for_select` is a nested array where each element has two elements: option text (city name) and option value (city id). The option value is what will be submitted to your controller. Often this will be the id of a corresponding database object but this does not have to be the case. | ||
|
||
Knowing this, you can combine `select_tag` and `options_for_select` to achieve the desired, complete markup: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The flow here was better before with this phrase. The guides should not be mechanical but have a nice flow
guides/source/form_helpers.md
Outdated
``` | ||
|
||
The first argument to `options_for_select` is a nested array where each element has two elements: option text (city name) and option value (city id). The option value is what will be submitted to your controller. Often this will be the id of a corresponding database object but this does not have to be the case. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the Often
here was a nice explanation and we should keep.
guides/source/form_helpers.md
Outdated
``` | ||
|
||
Whenever Rails sees that the internal value of an option being generated matches this value, it will add the `selected` attribute to that option. | ||
|
||
WARNING: When `:include_blank` or `:prompt` are not present, `:include_blank` is forced true if the select attribute `required` is true, display `size` is one and `multiple` is not true. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If it is complicated and incorrect we fix, we don't have to remove.
guides/source/form_helpers.md
Outdated
|
||
```erb | ||
<%= time_zone_select(:person, :time_zone) %> | ||
``` | ||
|
||
There is also `time_zone_options_for_select` helper for a more manual (therefore more customizable) way of doing this. Read the [API documentation](http://api.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelper.html#method-i-time_zone_options_for_select) to learn about the possible arguments for these two methods. | ||
|
||
Rails _used_ to have a `country_select` helper for choosing countries, but this has been extracted to the [country_select plugin](https://github.com/stefanpenner/country_select). When using this, be aware that the exclusion or inclusion of certain names from the list can be somewhat controversial (and was the reason this functionality was extracted from Rails). | ||
Rails _used_ to have a `country_select` helper for choosing countries, but this has been extracted to the [country_select gem](https://github.com/stefanpenner/country_select). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
plugin
is correct.
guides/source/form_helpers.md
Outdated
|
||
Uploading Files | ||
--------------- | ||
|
||
A common task is uploading some sort of file, whether it's a picture of a person or a CSV file containing data to process. The most important thing to remember with file uploads is that the rendered form's encoding **MUST** be set to "multipart/form-data". If you use `form_for`, this is done automatically. If you use `form_tag`, you must set it yourself, as per the following example. | ||
A common task is uploading some sort of file, whether it's a picture of a person or a CSV file containing data to process. The most important thing to remember with file uploads is that the rendered form's enctype **must** be set to "multipart/form-data". If you use `form_for`, this is done automatically. If you use `form_tag`, you must set it yourself, as per the following example. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
either encoding
as it was before or enctype attribute
.
e8e7f74
to
97c9a8d
Compare
rails/actionview/lib/action_view/helpers/tags/base.rb Lines 144 to 161 in 15774d2
It only works for select :
but doesn't for select_tag :rails/actionview/lib/action_view/helpers/form_tag_helper.rb Lines 135 to 158 in 15774d2
|
97c9a8d
to
02b56ee
Compare
This commit is the next work after rails#33523. Also, this commit removes mention about hidden `utf8` input. Since form helpers don't generate this input by default since rails#32125. Note that I also had created PR rails#31972 with improvements to "Action View Form Helpers" guide, but I'll rebase it after merging the current PR.
99e9e86
to
c03dd15
Compare
c03dd15
to
841fc18
Compare
Details
@@ -30,7 +30,7 @@ - Change "`` tag" to "form tag" - Change "`/home/index`" to "a home page" - Remove "(some line breaks added for readability)" since there aren't broken lines below.@@ -40,9 +40,11 @@
input
element" to "input elements" since there aretwo hidden elements.
input
is important, because non-GETform cannot be successfully submitted without it." to paragraph
about
authenticity_token
.security.html#cross-site-request-forgery-csrf
from "Security Guide" to "Securing Rails Application"(full name of the guide)
and append the word "guide".
@@ -76,10 +78,6 @@
text_field_tag
andsubmit_tag
,there is a similar helper for every form control in HTML." since
user is able to get this info from the guide below.
for search forms. This allows users to bookmark a specific search
and get back to it. More generally Rails encourages you to use
the right HTTP verb for an action".
@@ -110,7 +108,7 @@
Understanding Parameter Naming Conventions of this guide".
@@ -142,7 +140,7 @@
@@ -151,7 +149,7 @@
<%= label_tag(:age_adult, "I'm over 21") %>
outputs<label for="age_adult">I'm over 21</label>
I changed "I'm" to "I am" in input/output for consistency with example
in order to correct output.
@@ -215,7 +213,7 @@
security.html#logging
from "Security Guide" to "Securing Rails Application"(full name of the guide)
and append the word "guide".
@@ -233,10 +231,10 @@
"<input/>"
to"<input />"
for consistency with other examplesin the guide and readability.
params[:person]
hash is suitable forpassing to
Person.new
or, if@person
is an instance of Person,@person.update
. While the name of an attribute is the most commonsecond parameter to these helpers this is not compulsory.
In the example above, as long as person objects have a
name
and aname=
method Rails will be happy."@@ -283,7 +281,7 @@
Parameter Naming Conventions of this guide".
@@ -292,7 +290,7 @@
fields_for
with:contact_detail
argument to ensure that this will work if
@person.contact_detail
is
nil
.@@ -309,7 +307,9 @@
"(in fact
form_for
callsfields_for
internally)"We shouldn't express internals same as private API.
form_with
.@@ -319,7 +319,7 @@
(in order to consistency with the name of the guide) and
append the word "guide".
@@ -327,21 +327,21 @@
"short-style:" for consistency and removing useless info.
method:
argument in the example of usingform_for
from
html: { method: "patch"}
tomethod: "patch"
since it ismore correct and preferable.
enough to figure out if the record is new by asking
record.new_record?
"We shouldn't express internals same as private API.
the rest of this guide." since our examples show all attributes.
form_for
with STI:Change the sentence "You will have to specify the model name,
:url
,and
:method
explicitly." to "You will have to specify:url
,and
:as
(the model name) explicitly." since you'll have to specifyonly
:url
and:as
when use STI andform_for
.@@ -357,11 +357,11 @@
@@ -369,7 +369,7 @@
@@ -405,74 +404,84 @@
redundant
dots.database object but this does not have to be the case." and
useless example of "
select_tag
andoptions_for_select
":include_blank
or:prompt
are not present,
:include_blank
is forced true if the select attributerequired
is true, displaysize
is one andmultiple
is not true."that doesn't related to database.
@@ -480,21 +489,24 @@
select
with an association more clear to expresswhat is important to remember. Remove useless and complicated explanation
what would happen if you do wrong in this case.
@@ -511,7 +523,7 @@
@@ -520,16 +532,16 @@
ActiveSupport::TimeZone
" and express it as a link@@ -537,21 +549,21 @@
@@ -585,9 +600,12 @@
@@ -604,30 +622,28 @@
they do not aid the user in working out the relationship between
the date and the day of the week."
the explanation more clear by removing "if the current year is 2009"
clear.
@@ -631,38 +647,32 @@
url: {action: :upload}
to clarify the example sincebelow we use
upload
actioncannot set a default value for file inputs as this would have no meaning."
in order to pay attention of a reader to main info in this paragraph.
represent uploaded file by refer to
ActionDispatch::Http::UploadedFile
.uploaded_io
touploaded_file
.Remove useless section "Dealing with Ajax" since we can rely on
"Active Storage Overview" guide.
Change
FormBuilder
toActionView::Helpers::FormBuilder
andexpress it as a link.
@@ -703,12 +713,12 @@
FormBuilder
toActionView::Helpers::FormBuilder
.@@ -756,16 +766,19 @@
for consistency.
new hash whenever it encounters an input name that
already exists in the current hash" and "When working with array
parameters this duplicate submission will confuse Rails since duplicate input
names are how it decides when to start a new array element.
It is preferable to either use
check_box_tag
or to use hashes instead of arrays.".@@ -788,6 +801,7 @@
"<input name="_method" type="hidden" value="patch" />"
since this example of edit form.
@@ -812,7 +826,7 @@
:index
option, changeaddress
toaddress.id
.@@ -820,12 +834,12 @@
:index
option, changeaddress
toaddress.id
.@@ -862,7 +876,7 @@
@@ -948,7 +962,7 @@
input is placed in a location where an input tag is not valid HTML or when
using an ORM where children do not have an
id
".@@ -979,9 +993,9 @@
_destroy
.@@ -1024,4 +1038,4 @@
express as link the word "epoch" that can help to get to know about the "epoch".