Is there a sticker with radio buttons?

In accordance with (somewhat official) this manual , I can make a shortcut with a radio button by making a choice for this switch. They say

Always use labels for each check box and radio button. They associate text with a specific option and provide a large area with clicks .

Unfortunately, I could not get this functionality for my form. Here is my code:

<% form_for(@bet) do |f| %>
  <%= f.label :bet_type %>
  <%= f.radio_button :bet_type, "moneyline" %>
  <%= f.label :bet_type_moneyline, "Moneyline" %>
  <%= f.radio_button :bet_type, "spread" %>
  <%= f.label :bet_type_spread, "Spread" %>
<% end %>

I also tried this (because this is an example of using FormTagHelper instead of FormHelper):

<% form_for(@bet) do |f| %>
  <%= radio_button_tag :bet_type, "moneyline" %>
  <%= label_tag :bet_type_moneyline, "Moneyline" %>
  <%= radio_button_tag :bet_type, "spread" %>
  <%= label_tag :bet_type_spread, "Spread" %>
<% end %>

But I still can't get it to work. I use rails 2.3.5 and ruby ​​1.8.7.

Thanks for reading and maybe for the help!

+5
5

, :

<label>
  <input />
  This is an input!
</label>

.

Rails label , :

<%= f.label :moneyline do %>
  <%= f.radio_button :bet_type, "moneyline" %>
  "Moneyline"
<% end %>
+6

, . Rails 4 +.

, Rails ' FormHelper, "value" .

label(:post, :privacy, "Public Post", value: "public")
# => <label for="post_privacy_public">Public Post</label>

-

<%= f.radio_button :bet_type, "moneyline" %>
<%= f.label :bet_type, "Moneyline", value: "moneyline" %>
<%= f.radio_button :bet_type, "spread" %>
<%= f.label :bet_type, "Spread", value: "spread" %>

, :)

+6
<%= f.radio_button :bet_type, "moneyline" %>
<%= f.label :moneyline, "Moneyline", :for => "bet_type_moneyline" %>
+4

, HTML : :

<input id="bet_type_moneyline" name="bet_type" type="radio" value="moneyline">
<label for="bet_type_moneyline">Moneyline</label>

, , HTML, ... name = "bet_type type =" radio "...

-, for = "" id .

bet_type!

, !

+3

Woops! . - .

... no copy-paste. , , , , , (, -, ).

, - , ! , ! ?!

, . , , input.id label.for html. :

<%= f.radio_button :bet_type, "moneyline" %>
<%= f.label :moneyline, "Moneyline" %>

:

 <input id="bet_type_moneyline" name="bet_type type="radio" value="moneyline" />
 <label for="moneyline">Moneyline</label>

, input.id label.for ?

And only when I ran the code in my question did I get it right.

It seems to work like this: the radio button tag method makes input.id (object_name + "_" + value), and the label label method makes label.for object_name from it. And when these two are equal, you get a shortcut of choice.

I hope this discovery can help someone else get out of line.

Sorry for the fact that your gears don't do anything either!

0
source

All Articles