Labels for radio buttons in rails form

前端 未结 5 989
天命终不由人
天命终不由人 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条回答
  •  Happy的楠姐
    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

提交回复
热议问题