How to use radio button correctly in rails?

后端 未结 2 1532
耶瑟儿~
耶瑟儿~ 2020-12-23 20:06

I\'m trying to create some radio buttons and I\'m not sure how to. Following this question I\'ve got it set up working almost correct, but I\'m new to this and not sure why

相关标签:
2条回答
  • 2020-12-23 20:24

    see label(object_name, method, content_or_options = nil, options = nil, &block)

      <div class="field">
        <%= f.label :autolyse %><br />
        <%= f.label :autolyse, "Yes", :value => "true"  %>
        <%= f.radio_button :autolyse, true %>
        <%= f.label :autolyse, "No", :value => "false" %>
        <%= f.radio_button :autolyse, false, :checked => true %>
      </div>
    
    0 讨论(0)
  • 2020-12-23 20:25

    If you want to keep selected the option chosen by the user, you should validate the param, would be something like this:

    <div class="field">
      <%= f.label :autolyse %><br />
      <%= f.label :autolyse, "Yes", :value => "true"  %>
      <%= f.radio_button :autolyse, true, !!params[:autolyse] %>
      <%= f.label :autolyse, "No", :value => "false" %>
      <%= f.radio_button :autolyse, false, !!params[:autolyse] %>
    </div>
    

    If you want to do it from the object properties you just replace the params variable for the object property:

    <div class="field">
      <%= f.label :autolyse %><br />
      <%= f.label :autolyse, "Yes", :value => "true"  %>
      <%= f.radio_button :autolyse, true, !!@object.autolyse %>
      <%= f.label :autolyse, "No", :value => "false" %>
      <%= f.radio_button :autolyse, false, !!@object.autolyse %>
    </div>
    
    0 讨论(0)
提交回复
热议问题