Note that there are some explanatory texts on larger screens.

plurals
  1. POLabels for radio buttons in rails form
    text
    copied!<p>My question is similar to <a href="https://stackoverflow.com/questions/658689/how-to-associate-labels-with-radio-buttons">this one</a> but for a Rails app.</p> <p>I have a form with some radio buttons, and would like to associate labels with them. The <code>label</code> form helper only takes a form field as a parameter, but in this case I have multiple radio buttons for a single form field. The only way I see to do it is to manually create a label, hard coding the ID that is auto generated for the radio button. Does anyone know of a better way to do it?</p> <p>For example:</p> <pre><code>&lt;% form_for(@message) do |f| %&gt; &lt;%= label :contactmethod %&gt; &lt;%= f.radio_button :contactmethod, 'email', :checked =&gt; true %&gt; Email &lt;%= f.radio_button :contactmethod, 'sms' %&gt; SMS &lt;% end %&gt; </code></pre> <p>This generates something like:</p> <pre><code>&lt;label for="message_contactmethod"&gt;Contactmethod&lt;/label&gt; &lt;input checked="checked" id="message_contactmethod_email" name="message[contactmethod]" value="email" type="radio"&gt; Email &lt;input id="message_contactmethod_sms" name="message[contactmethod]" value="sms" type="radio"&gt; SMS </code></pre> <p>What I want:</p> <pre><code>&lt;input checked="checked" id="message_contactmethod_email" name="message[contactmethod]" value="email" type="radio"&gt;&lt;label for="message_contactmethod_email"&gt;Email&lt;/label&gt; &lt;input id="message_contactmethod_sms" name="message[contactmethod]" value="sms" type="radio"&gt; &lt;label for="message_contactmethod_sms"&gt;SMS&lt;/label&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload