-
Notifications
You must be signed in to change notification settings - Fork 21.4k
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
Remove duplicate stringify_keys in text_field_tag and number_field_tag #17271
Remove duplicate stringify_keys in text_field_tag and number_field_tag #17271
Conversation
If the change is the same but in different methods it is better to keep in
|
I don't like the idea of having two |
@rafaelfranca – yes, that was my point: Hashes are now ordered by insertion order in Ruby. That is the rationale behind the PR. |
I guess I may be too afraid about it. I'll merge but I prefer to include #17272 in the same commit. |
@rafaelfranca – ok, I'll merge! |
Could you squash your commits? |
All the methods that invoke `text_field_tag` (such as `hidden_field_tag`) and all the methods that invoke `number_field_tag` (that is `range_field_tag`) do not need to call `stringify_keys` on their `options` parameter since the `text_field_tag` method [is already doing it internally](https://github.com/claudiob/rails/blob/4159134524f4c78d008eef9d9a17f73a3172dcc8/actionview/lib/action_view/helpers/form_tag_helper.rb#L182): ```ruby def text_field_tag(name, value = nil, options = {}) tag :input, { "type" => "text", "name" => name, "id" => sanitize_to_id(name), "value" => value }.update(options.stringify_keys) end ``` and `number_field_tag` is [already doing it internally](https://github.com/claudiob/rails/blob/e3207bdbba55f3806441f22b175557579bc0b051/actionview/lib/action_view/helpers/form_tag_helper.rb#L780) as well: ```ruby def number_field_tag(name, value = nil, options = {}) options = options.stringify_keys ... end [Note #1: My code uses `merge` to respect the existing behavior of duplicating the `options` hash before updating its keys, see rails#17096 (comment)] [Note #2: My code uses symbols instead of strings (e.g.: `:hidden`) to look forward to future version of Ruby/Raiks (GC symbols); the result of the method, however, is the same, because the symbols are stringified inside `text_field_tag`] [Note #3: I had previously created a similar PR rails#17096 but decided to split it into multiple PRs given the feedback received in the comments]
769b8ea
to
a9050e7
Compare
Remove duplicate stringify_keys in text_field_tag and number_field_tag
@rafaelfranca Squashed! On a side note, compared to my old PR, what I removed is dbf2dbe because those lines of code actually change the behavior of the methods Currently, these two helpers are different from all the other ones in that they modify the Therefore, if I use <% options = {class: 'important'} %>
<%= text_field_tag 'Name', options %>
<%= file_field_tag 'Upload', options %> I correctly get a text field and a file field: <input class="important" id="Name" name="Name" type="text" value="value">
<input class="important" id="Upload" name="Upload" type="file"> but if I use <% options = {class: 'important'} %>
<%= file_field_tag 'Upload', options %>
<%= text_field_tag 'Name', options %> then I get two file fields! <input class="important" id="Upload" name="Upload" type="file">
<input class="important" id="Name" name="Name" type="file" value="value"> I think this is a bug… if you agree I can make a new PR with that commit to fix it! What do you think? |
Yes. It is a bug. Lets fix it. |
All the methods that invoke
text_field_tag
(such ashidden_field_tag
) do not need to callstringify_keys
on theiroptions
parameter since thetext_field_tag
method is already doing it internally:[Note 1: My code uses
merge
to respect the existing behavior of duplicating theoptions
hash before updating its keys, see https://github.com//pull/17096#issuecomment-57223827][Note 2: My code uses symbols instead of strings (e.g.:
:hidden
) to look forward to future version of Ruby/Raiks (GC symbols); the result of the method, however, is the same, because the symbols are stringified insidetext_field_tag
][Note 3: I had previously created a similar PR #17096 but decided to split it into multiple PRs given the feedback received in the comments]