Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

select helper breaks for numerical values and displays

#3203

If I have builder.select(:method, [1,2,3]) etc, I get
123

This is what I would expect. However if my_instance.method returns 2, the options with value, "2", does not get selected.

However if I do,

builder.select(:method, [1,2,3].collect{|e| [e, "a#{e}"]})

thus appending an 'a' before the value of each element, the behavior works as expected.

I think that the method should match on "2" == "2" and put out selected="selected" and not require an alpha character prefix.

Thanks in advance.

Sam

Reported by Sam Woodard · September 14th, 2009 @ 11:52 PM

State: invalid
Milestone: none
Assigned to: nobody
Importance: none

Activity

  1. CancelProfileIsBroken
  2. Alex Kahn
    Alex Kahn

    I can reproduce this. What Sam is saying is that when using the select method in a form_for block and viewing an edit action on a resource, the "selected" option is set if the value for the field is a String but not if it's a Fixnum.

    So, if Post.category == 'bar', and the view code is f.select :category, ['foo', 'bar', 'baz'], bar will be selected when you edit the post.

    But if Post.category == 3, and the view code is f.select :category, [1,2,3], none of the options in the select will be marked 'selected'.

    September 26th, 2009 @ 03:06 AM

  3. Elad Meidar
    Elad Meidar

    -1 unable to reproduce, maybe a failing test would make some sense into it.

    September 26th, 2009 @ 06:56 AM

  4. Elad Meidar
    Elad Meidar

    @Sam: also, if you use options_for_select([1,2,3], :selected => 2) it's working just fine. besides, it seems like you are trying to create a element that is not related to an ActiveRecord instance attribute, if that's the case... why not code it as plain HTML ?

    September 26th, 2009 @ 07:04 AM

  5. Elomar França
    Elomar França

    Unable to reproduce, neither in 2-3-stable nor in master.

    September 26th, 2009 @ 04:35 PM

  6. sr.iniv.t
    sr.iniv.t

    -1 not reproducible on 2-3-stable.

    I have attached a patch to verify that select tag chooses the right option even if the field is numeric. I'm a newbie, so can someone please verify the patch and let me know if the test is right? Thanks!

    September 26th, 2009 @ 07:08 PM

  7. Elad Meidar
    Elad Meidar

    @sr.iniv.t - is this patch for 2-3-stable ?

    September 26th, 2009 @ 07:29 PM

  8. sr.iniv.t
  9. John Trupiano
    John Trupiano

    @sr.iniv.t this patch does not apply cleanly for me. I have attached a new patch.

    September 26th, 2009 @ 09:44 PM

  10. Ben Marini
    Ben Marini

    However if my_instance.method returns 2, the options with value, "2", does not get selected. @sam to clarify, does my_instance.method return a String or a Fixnum? I'm guessing it returns a string, and that's why it doesn't work. You could probably fix it with f.select :category, %w(1 2 3)

    September 27th, 2009 @ 12:21 AM

  11. Ben Marini
    Ben Marini

    Whoops let me try that again...

    << However if my_instance.method returns 2, the options with value, "2", does not get selected.

    @sam to clarify, does my_instance.method return a String or a Fixnum? I'm guessing it returns a string, and that's why it doesn't work. You could probably fix it with

    f.select :category, %w(1 2 3)
    

    September 27th, 2009 @ 12:23 AM

  12. Sam Woodard
    Sam Woodard
    • Tag cleared.

    This is a bug that I have been able to reproduce over and over again, for those of you that say it is not. Can we resume this conversation? This bug is quite annoying. I apologize that I reported the bug and then did not follow up.

    January 10th, 2010 @ 05:23 PM

  13. Sam Woodard
    Sam Woodard

    seems to be fixed in 2.3.5

    January 10th, 2010 @ 05:39 PM

  14. Rohit Arondekar
    Rohit Arondekar

    Can somebody confirm if this issue is resolved in rails 2.3.8 and whether this ticket can be closed?

    June 17th, 2010 @ 08:25 AM

  15. José Valim
    José Valim
    • State changed from new to invalid

    Please add a comment if this issue still exists.

    June 22nd, 2010 @ 04:34 PM

  16. bingbing