#16 new
Luke Sutton

select/radio_button helpers don't bind correctly

Reported by Luke Sutton | November 26th, 2008 @ 08:43 PM

When using form_for, the bound versions of #select and #radio_button helpers don't select the correct value based on the relevant model property. Instead I have explicitly pass in the selected value.

Comments and changes to this ticket

  • cult hero

    cult hero December 17th, 2008 @ 04:53 PM

    It appears check_box has similar problems. It's always checked. I have some feeling these are related.

  • cult hero

    cult hero December 17th, 2008 @ 07:34 PM

    "Feelings" are bad I guess. The problems are unrelated. (I patched the other one.) In fact, I can't duplicate the problem. Do you have any more details? Select is working fine for me.

  • felix (xilef)

    felix (xilef) January 13th, 2009 @ 10:21 PM

    I have an issue with this, I need to specify to_s on the selected property for it to work ie:

    select :listing_type_id, :collection => ..., :text_method => :name, :value_method => :id, :selected => @listing.listing_type_id.to_s

    Without the ':selected => 'attribute it doesn't work so I have to specify it. Even then without the 'to_s' method it doesn't work.

  • Lang Riley

    Lang Riley January 23rd, 2009 @ 02:05 PM

    • Tag set to form, radio_button, radio_group

    radio_button and radio_group have been broken for a long time with regard to setting checked state. I filed a bug on this months ago. I decided to try it again today to see if it has been fixed, rather than writing markup directly and sure enough, it is still broken. I went into detail in the ticket I filed months ago and do not wish to here again for the sake of time, but suffice to say browsers behave properly when only the "checked" string is emitted for a radio button that is to be checked and not "checked=checked" or "checked=true" and certainly not emitting the "checked" string in any way, including "checked=false" on the radio button that is not to be checked. The difficulty with working around this by writing your own markup is if you are using haml which uses associations to build element attributes. I hope this gets fixed soon. I realize the difficulty is that "checked" by itself does not lend itself to the key value pair paradigm.

  • Lang Riley

    Lang Riley January 23rd, 2009 @ 02:30 PM

    Here is a workaround of said problem in haml by using an erb filter:

                :erb
                  <input type="radio" name="user[contact_by_phone]" value="false" <%= !@user.contact_by_phone ? "checked" : "" %> />
                %label.by-email{ :for => "user_contact_by_phone" }
                  by email?
                :erb
                  <input type="radio" name="user[contact_by_phone]" value="true" <%= @user.contact_by_phone ? "checked" : "" %> />
                %label{ :for => "user_contact_by_phone" }
                  by phone?
    
    
  • Lang Riley

    Lang Riley January 23rd, 2009 @ 02:34 PM

    Ok, I have to amend one detail that I had wrong: "checked=checked" will suffice as long as the "checked" string does not get emitted on a radio button that is not supposed to be checked, e.g., this is incorrect => "checked=false". The browser just sees "checked" in this case and if it happens to be the last radio button in a group it sets it to the checked state.

Please Sign in or create a free account to add a new ticket.

With your very own profile, you can contribute to projects, track your activity, watch tickets, receive and update tickets through your email and much more.

New-ticket Create new ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

Shared Ticket Bins

People watching this ticket

Pages