You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have a situation that requires me to explicitly define the named argument index: passed to a call of form_builder.text_field. The value for index is either set or not. If it is set (not nil), then it should take that and place the [] infix with the value inside the brackets, for the name attribute of the <input> element.
Steps to reproduce
Have a template that uses form_for and under that place a call to: form.text_field with an index: nil option.
<%= form_for :some_model do |form| %>
<%= form.text_field :something, index: nil %>
<% end %>
Steps tracing the output for the name attribute that contains the "index":
Note index is nil in the hash above, options. Since the key index is defined, it's taking the nil value, but due to short circuit with || it makes the function return empty string instead of nil.
[2] pry(#<ActionView::Helpers::Tags::TextField>)> up
From: [...]/lib/action_view/helpers/tags/base.rb:96 ActionView::Helpers::Tags::Base#add_default_name_and_id:
95: def add_default_name_and_id(options)
=> 96: index = name_and_id_index(options)
97: options["name"] = options.fetch("name") { tag_name(options["multiple"], index) }
98:
99: if generate_ids?
100: options["id"] = options.fetch("id") { tag_id(index) }
101: if namespace = options.delete("namespace")
102: options["id"] = options["id"] ? "#{namespace}_#{options['id']}" : namespace
103: end
104: end
105: end
Because index is passed as empty string (and not nil), it goes to the case when index, since empty string IS NOT FALSY in Ruby.
That results in an infix [] being added to the name attribute string. That would make sense if I had originally passed index: "", but here originally I passed index: nil. So I don't think this is the appropriate or correct behavior.
This issue has been automatically marked as stale because it has not been commented on for at least three months.
The resources of the Rails team are limited, and so we are asking for your help.
If you can still reproduce this error on the 6-1-stable branch or on main, please reply with all of the information you have about it in order to keep the issue open.
Thank you for all your contributions.
I have a situation that requires me to explicitly define the named argument
index:
passed to a call ofform_builder.text_field
. The value forindex
is either set or not. If it is set (not nil), then it should take that and place the[]
infix with the value inside the brackets, for the name attribute of the<input>
element.Steps to reproduce
Have a template that uses
form_for
and under that place a call to:form.text_field
with anindex: nil
option.Steps tracing the output for the name attribute that contains the "index":
rails/actionview/lib/action_view/helpers/tags/base.rb
Lines 173 to 175 in cf99be4
Note
index
is nil in the hash above,options
. Since the keyindex
is defined, it's taking the nil value, but due to short circuit with||
it makes the function return empty string instead ofnil
.rails/actionview/lib/action_view/helpers/tags/base.rb
Line 96 in cf99be4
index
in line 96 is then set to empty string""
, and a call totag_name
is made with the empty string as second argument.rails/actionview/lib/action_view/helpers/tags/base.rb
Lines 107 to 113 in cf99be4
Because index is passed as empty string (and not
nil
), it goes to the casewhen index
, since empty string IS NOT FALSY in Ruby.That results in an infix
[]
being added to the name attribute string. That would make sense if I had originally passedindex: ""
, but here originally I passedindex: nil
. So I don't think this is the appropriate or correct behavior.Expected behavior
index: nil
should not add an infix[]
to the name attribute of the elementIn contrast,
index: ""
should place the empty-bracket infix[]
(this already happens)Actual behavior
index: nil
is adding the infix[]
to the name attribute of the elementSystem configuration
Rails version: Rails 7.0.0.alpha.790723b
Ruby version: 3.0.0
The text was updated successfully, but these errors were encountered: