Form helper: Linking radio button to label

34 views
Skip to first unread message

Ronald Fischer

unread,
Jun 5, 2014, 8:49:47 AM6/5/14
to rubyonra...@googlegroups.com
My form essentially looks like this:

<%= form_for :xxx, url: xxxs_path, method: 'get', enforce_utf8:true do
|d| %>
<%= d.text_field('filter', value: @filter, maxlength:64, size:16) %>
<%= d.radio_button(:filtertype, 'regexp') %>
<%= d.label :filtertype_regexp, 'Regular Expression', value: 'regexp'
%>
<%= d.radio_button(:filtertype, 'lefteq', checked:true) %>
<%= d.label :filtertype_lefteq, 'Starts with...', value: 'lefteq' %>
<%= d.submit(value="FILTER LIST BELOW", name: 'filter') %>
<% end %>

I thought from this association between radio buttons and label,
clicking on *text* (i.e. 'Regular Expression' should already cause the
button to be selected, but this is not the case. Did I miss something
here?

--
Posted via http://www.ruby-forum.com/.

Walter Lee Davis

unread,
Jun 5, 2014, 10:24:19 AM6/5/14
to rubyonra...@googlegroups.com
Look at the generated HTML in a browser. In order for the label to affect the radio button, one of two things must be true:

1. Either the label's "for" attribute exactly matches the radio button's "id" attribute, or
2. The label wraps around the button and does not have a "for" attribute.

Sometimes you have to get out and push a little with radio buttons, although looking at it briefly, you appear to be doing the right things by having the same name input into both the label and the radio.

Walter

>
> --
> Posted via http://www.ruby-forum.com/.
>
> --
> You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-ta...@googlegroups.com.
> To post to this group, send email to rubyonra...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/fb20194dc119e820ab3d7d624b52dbbb%40ruby-forum.com.
> For more options, visit https://groups.google.com/d/optout.

Ronald Fischer

unread,
Jun 9, 2014, 6:35:40 AM6/9/14
to rubyonra...@googlegroups.com
Walter Davis wrote in post #1148961:
> Look at the generated HTML in a browser. In order for the label to
> affect the radio button, one of two things must be true:
>
> 1. Either the label's "for" attribute exactly matches the radio button's
> "id" attribute, or

Got it! I now can see my mistake. The label should be

<%= d.label :filtertype_lefteq, 'Starts with...', value: 'lefteq' %>

> 2. The label wraps around the button and does not have a "for"
> attribute.


Just out of curiosity: How can *that* be done inside an ERB file?

Ronald

Walter Lee Davis

unread,
Jun 9, 2014, 9:01:03 AM6/9/14
to rubyonra...@googlegroups.com
<%= f.label :your_element do %>
<%= f.check_box :your_element %>
The label text for your element
<%- end %>

That creates a wrapped label. I use this all the time, particularly when making a Bootstrap layout.

Walter

>
> Ronald
>
> --
> Posted via http://www.ruby-forum.com/.
>
> --
> You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-ta...@googlegroups.com.
> To post to this group, send email to rubyonra...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/f65c4a2420c35a32b3668128d54b120d%40ruby-forum.com.
Reply all
Reply to author
Forward
0 new messages