Ruby-on-rails rails 形式的单选按钮标签

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/746387/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 21:07:52  来源:igfitidea点击:

Labels for radio buttons in rails form

ruby-on-railsforms

提问by Bryan

My question is similar to this onebut for a Rails app.

我的问题与类似, 但针对 Rails 应用程序。

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

我有一个带有一些单选按钮的表单,并希望将标签与它们相关联。的label形式辅助只需表单字段作为参数,但在这种情况下,我有一个单一的表单字段多个单选按钮。我看到的唯一方法是手动创建一个标签,硬编码为单选按钮自动生成的 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>

采纳答案by Matt Haley

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

回答by John Douthat

Passing the :valueoption to f.labelwill ensure the label tag's forattribute is the same as the id of the corresponding radio_button

传递:value选项f.label将确保标签标签的for属性与对应的 id 相同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

参见ActionView::Helpers::FormHelper#label

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

:value 选项,旨在针对 radio_button 标签的标签

回答by James Conroy-Finn

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

如果您希望 object_name 以任何 ID 为前缀,您应该在表单对象上调用表单助手:

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

如果您无法在表单对象上调用表单助手方法,例如,如果您使用标签助手(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.

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

回答by localhostdotdev

Using true/falseas the value will have the field pre-filled if the model passed to the form has this attribute already filled:

如果传递给表单的模型已填充此属性,则使用true/false作为值将预填充该字段:

= f.radio_button(:public?, true)
= f.label(:public?, "yes", value: true)
= f.radio_button(:public?, false)
= f.label(:public?, "no", value: false)