Labels for radio buttons in rails form

前端 未结 5 979
天命终不由人
天命终不由人 2020-12-04 06:15

My question is similar to this one but for a Rails app.

I have a form with some radio buttons, and would like to associate labels with them. The label f

相关标签:
5条回答
  • 2020-12-04 06:23

    Using true/false as the value will have the field pre-filled if the model passed to the form has this attribute already filled:

    = f.radio_button(:public?, true)
    = f.label(:public?, "yes", value: true)
    = f.radio_button(:public?, false)
    = f.label(:public?, "no", value: false)
    
    0 讨论(0)
  • 2020-12-04 06:32

    If you want the object_name prefixed to any ID you should call form helpers on the form object:

    - form_for(@message) do |f|
      = f.label :email
    

    This also makes sure any submitted data is stored in memory should there be any validation errors etc.

    If you can't call the form helper method on the form object, for example if you're using a tag helper (radio_button_tag etc.) you can interpolate the name using:

    = radio_button_tag "#{f.object_name}[email]", @message.email
    

    In this case you'd need to specify the value manually to preserve any submissions.

    0 讨论(0)
  • 2020-12-04 06:37

    Passing the :value option to f.label will ensure the label tag's for attribute is the same as the id of the corresponding radio_button

    <% form_for(@message) do |f| %>
      <%= f.radio_button :contactmethod, 'email' %> 
      <%= f.label :contactmethod, 'Email', :value => 'email' %>
      <%= f.radio_button :contactmethod, 'sms' %>
      <%= f.label :contactmethod, 'SMS', :value => 'sms' %>
    <% end %>
    

    See ActionView::Helpers::FormHelper#label

    the :value option, which is designed to target labels for radio_button tags

    0 讨论(0)
  • 2020-12-04 06:46
    <% form_for(@message) do |f| %>
      <%= f.radio_button :contactmethod, 'email', :checked => true %> 
      <%= label :contactmethod_email, 'Email' %>
      <%= f.radio_button :contactmethod, 'sms' %>
      <%= label :contactmethod_sms, 'SMS' %>
    <% end %>
    
    0 讨论(0)
  • 2020-12-04 06:47

    This an example from my project for rating using radio buttons and its labels

    <div class="rating">
      <%= form.radio_button :star, '1' %>
      <%= form.label :star, '☆', value: '1' %>
    
      <%= form.radio_button :star, '2' %>
      <%= form.label :star, '☆', value: '2' %>
    
      <%= form.radio_button :star, '3' %>
      <%= form.label :star, '☆', value: '3' %>
    
      <%= form.radio_button :star, '4' %>
      <%= form.label :star, '☆', value: '4' %>
    
      <%= form.radio_button :star, '5' %>
      <%= form.label :star, '☆', value: '5' %>
    </div>
    
    0 讨论(0)
提交回复
热议问题