rails 4 radio_button_tag默认未选中

时间:2015-08-14 04:11:21

标签: ruby-on-rails

我正在使用Rails 4,但我无法将radio_button_tag设置为默认的第一个选项。

.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"

4 个答案:

答案 0 :(得分:0)

查看doc for the radio_button_tag

这应该有效:

.field
    =radio_button_tag :type, "order", true, class: "rfi_radio"

答案 1 :(得分:0)

根据http://apidock.com/rails/ActionView/Helpers/FormTagHelper/radio_button_tag,您应该传入布尔值而不是哈希值。

=radio_button_tag :type, "order", true, class: "rfi_radio"

答案 2 :(得分:0)

Rails默认将默认单选按钮设置为已选中。我必须将所有备选项设置为false才能正常工作。

.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"

答案 3 :(得分:0)

我花了一个小时才弄清楚这一点,所以想分享一下,以防其他人遇到问题:

  • radio_button_tagchecked参数将单选按钮显示为已选中状态,无论它作为参数提供了什么(除非nilfalse),因为大多数其他情况is "truthy"
  • 我必须向一组中的 恰好一个 个单选按钮提供checked参数。(组是具有相同名称的那些单选按钮)。 / li>

希望这些提示对您有所帮助!