2

I'm using Rails 4 but I can't get the radio_button_tag to set the first option as default.

.field
  =radio_button_tag :type, "order", checked:"true", class: "rfi_radio"
  =label_tag :type_order, "Information pertaining to an order"
  br
  =radio_button_tag :type, "quote", class: "rfi_radio"
  =label_tag :type_quote, "Information pertaining to a quote"
  br
  =radio_button_tag :type, "customer", class: "rfi_radio"
  =label_tag :type_customer, "Information pertaining to a customer"
  br
  =radio_button_tag :type, "general", class: "rfi_radio"
  =label_tag :type_general, "General information"
ekremkaraca
  • 1,453
  • 2
  • 18
  • 37
Suavocado
  • 949
  • 12
  • 27

4 Answers4

1

Look at the doc for the radio_button_tag.

This should work:

.field
    =radio_button_tag :type, "order", true, class: "rfi_radio"
Zentaurus
  • 758
  • 2
  • 11
  • 27
LolWalid
  • 515
  • 5
  • 15
1

According to http://apidock.com/rails/ActionView/Helpers/FormTagHelper/radio_button_tag, you should just pass in the boolean value and not a hash.

=radio_button_tag :type, "order", true, class: "rfi_radio"
elements
  • 1,047
  • 2
  • 9
  • 21
  • I changed the line to =radio_button_tag :rfi_type, "order", true, class: "rfi_radio" but it didn't change anything. – Suavocado Aug 14 '15 at 04:46
  • What is the HTML generated by the tag? – elements Aug 14 '15 at 05:10
  • It's a little bit late but maybe recheck all your radio buttons name. There could be another radio button with the same name and have the attribute `checked` too. In that case the last one wins and you will see your first radio button being unchecked even though its HTML does have `checked` inside. – DDMC Oct 13 '22 at 09:33
0

Rails was setting the default radio buttons as checked by default. I had to set all the alternates as false before it would work correctly.

.field
  =radio_button_tag "type", "order", true,  class: "rfi_radio"
  =label_tag :type_order, "Information pertaining to an order"
  br
  =radio_button_tag "type", "quote", false, class: "rfi_radio"
  =label_tag :type_quote, "Information pertaining to a quote"
  br
  =radio_button_tag "type", "customer", false, class: "rfi_radio"
  =label_tag :type_customer, "Information pertaining to a customer"
  br
  =radio_button_tag "type", "general", false, class: "rfi_radio"
  =label_tag :type_general, "General information"
Suavocado
  • 949
  • 12
  • 27
0

I just spent an hour figuring this out, so want to share in case it helps anyone else:

  • The radio_button_tag's checked parameter will show the radio button as checked no matter what it's provided as the argument (unless nil or false) since most everything else is "truthy".
  • I had to provide checked parameter to exactly one of the radio buttons in a group (group being those radio buttons with the same name).

Hope these tips help!

stevec
  • 41,291
  • 27
  • 223
  • 311