在rails窗体上的单选按钮的标签。

时间:2022-05-29 00:26:02

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

我的问题与这个类似,但对于Rails应用来说。

I have a form with some radio buttons, and would like to associate labels with them. The label 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?

我有一个带有一些单选按钮的表单,并且想要将标签与它们联系起来。标签表单帮助器只接受一个表单字段作为参数,但在本例中,我为一个表单字段拥有多个单选按钮。我看到的唯一方法是手工创建一个标签,硬编码为单选按钮自动生成的ID。有人知道更好的方法吗?

For example:

例如:

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

This generates something like:

这产生类似:

<label for="message_contactmethod">Contactmethod</label>
<input checked="checked" id="message_contactmethod_email" name="message[contactmethod]" value="email" type="radio"> Email
<input id="message_contactmethod_sms" name="message[contactmethod]" value="sms" type="radio"> SMS

What I want:

我想要的:

<input checked="checked" id="message_contactmethod_email" name="message[contactmethod]" value="email" type="radio"><label for="message_contactmethod_email">Email</label>
<input id="message_contactmethod_sms" name="message[contactmethod]" value="sms" type="radio"> <label for="message_contactmethod_sms">SMS</label>

3 个解决方案

#1


126  

<% 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 %>

#2


201  

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

将:value选项传递给f。标签将确保标签标签的属性与相应的radio_button的id相同。

<% 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

看到ActionView::助手::FormHelper #标签

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

value选项,它是为radio_button标签的标签设计的。

#3


1  

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

如果您希望object_name前缀为任何ID,则应在表单对象上调用表单helper:

- 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:

如果您不能在表单对象上调用form helper方法,例如,如果您使用的是标记助手(radio_button_tag等),您可以使用以下方法来插入名称:

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

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

在这种情况下,您需要手动指定值以保存任何提交。

#1


126  

<% 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 %>

#2


201  

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

将:value选项传递给f。标签将确保标签标签的属性与相应的radio_button的id相同。

<% 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

看到ActionView::助手::FormHelper #标签

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

value选项,它是为radio_button标签的标签设计的。

#3


1  

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

如果您希望object_name前缀为任何ID,则应在表单对象上调用表单helper:

- 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:

如果您不能在表单对象上调用form helper方法,例如,如果您使用的是标记助手(radio_button_tag等),您可以使用以下方法来插入名称:

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

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

在这种情况下,您需要手动指定值以保存任何提交。