This project is archived and is in readonly mode.
select tags do not check the :selected option properly
Reported by rk | July 6th, 2010 @ 11:53 PM | in 3.x
Hi,
When using the helpers which generate select tags and their options, I've run into a relatively universal problem.
<%= f.select :plan_id, ... %>
# and
<%= f.select :plan_id, ..., :selected => params[:subscription].try(:[], :plan_id) %>
Will both generate a nice big list of plans, but wont select one after being submitted (if errors appear or the form generates a preview first).
The simple fix is the following:
<%= f.select :plan_id, ..., :selected => params[:subscription].try(:[], :plan_id).to_i %>
Yup, that simple. The type that gets read from the record is Fixnum while the type read from the post is String, and of course no string equals a fixnum (by default). A little type coercion could go a ways to fixing this.
Comments and changes to this ticket
-
Subba July 16th, 2010 @ 09:15 PM
- Assigned user set to Neeraj Singh
-
Subba July 20th, 2010 @ 04:31 AM
adding patch to fix above problem this will coerce the selected , options values to string before comparison.
above problem also happens if the selected is float -
Neeraj Singh July 20th, 2010 @ 06:01 PM
- Milestone set to 3.x
- State changed from new to open
- Tag changed from formbuilder, forms, form_tag_helper to rails 3, formbuilder, forms, form_tag_helper
- Importance changed from to Low
Subba, patch looks good.
Can you write test such that there is no need to escape double quotes. It puts too much of strain. Replace
"<option selected=\"selected\" value=\"1\">first</option>\n<option value=\"2\">second</option>"
with
%(<option selected="selected" value="1">first</option>\n<option value="2">second</option>)
-
Neeraj Singh July 22nd, 2010 @ 07:25 PM
- Assigned user changed from Neeraj Singh to José Valim
+1 looks good to me
-
José Valim July 26th, 2010 @ 04:13 PM
Yo, thanks for the patch! I don't think there is a need to use try. This is in fact causing a bug today, imagine we have [nil] in selected, it will convert nil to string in the selected array, but not the value, because of try.
Also, I think we can convert these values to_s somewhere earlier since they will be converted anyway when we create (so we don't convert them twice). But this is rather secondary.
-
Repository August 14th, 2010 @ 12:23 PM
- State changed from open to resolved
(from [e5163784102e3ce97b99dd50b35b18868dc3c3b5]) Adding missing required statement
[#5056 state:resolved]
Signed-off-by: Santiago Pastorino santiago@wyeworks.com
http://github.com/rails/rails/commit/e5163784102e3ce97b99dd50b35b18... -
Subba August 14th, 2010 @ 02:10 PM
- Assigned user changed from José Valim to Santiago Pastorino
can you reopen the ticket. commit mention is #5317 ticket. i used wrong ticket number to create patch. this ticket still in review by jose.
-
Neeraj Singh August 14th, 2010 @ 02:33 PM
- State changed from resolved to open
-
Subba August 14th, 2010 @ 03:59 PM
- Assigned user changed from Santiago Pastorino to José Valim
above patch will no longer with recent changes. i am creating new patch this has less invasive changes than the one above.
-
Repository August 15th, 2010 @ 08:50 PM
- State changed from open to resolved
(from [54477c98482f37d547fa158155ae1624b1f7f7ac]) select tags coerce the :selected option, options to strings before comparison [#5056 state:resolved]
Signed-off-by: José Valim jose.valim@gmail.com
http://github.com/rails/rails/commit/54477c98482f37d547fa158155ae16... -
Repository August 15th, 2010 @ 08:55 PM
(from [dbe5ae488eebee660cd97e59a2e15c4752fbe15b]) select tags coerce the :selected option, options to strings before comparison [#5056 state:resolved]
Signed-off-by: José Valim jose.valim@gmail.com
http://github.com/rails/rails/commit/dbe5ae488eebee660cd97e59a2e15c...
Create your profile
Help contribute to this project by taking a few moments to create your personal profile. Create your profile »
<h2 style="font-size: 14px">Tickets have moved to Github</h2>
The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>
People watching this ticket
Attachments
Referenced by
- 5056 select tags do not check the :selected option properly [#5056 state:resolved]
- 5056 select tags do not check the :selected option properly (from [54477c98482f37d547fa158155ae1624b1f7f7ac]) select ...
- 5056 select tags do not check the :selected option properly (from [dbe5ae488eebee660cd97e59a2e15c4752fbe15b]) select ...