【问题标题】:Labels for radio buttons in rails form轨道形式的单选按钮标签
【发布时间】:2010-10-19 06:49:42
【问题描述】:

我的问题类似于this one 但是对于 Rails 应用程序。

我有一个带有一些单选按钮的表单,并希望将标签与它们相关联。 label 表单助手仅将表单字段作为参数,但在这种情况下,我有多个单选按钮用于单个表单字段。我看到的唯一方法是手动创建一个标签,硬编码为单选按钮自动生成的 ID。有谁知道更好的方法吗?

例如:

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

这会产生类似的东西:

<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

我想要什么:

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

【问题讨论】:

    标签: ruby-on-rails forms


    【解决方案1】:

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

    ActionView::Helpers::FormHelper#label

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

    【讨论】:

    • 好吧,这可能是 0 票,因为它在几个月后得到了回答,但实际上它是好的。警告:你需要 Rails >= 2.3.3
    • 此方法还会在必要时呈现field_with_errors div(使用:contactmethod_email 方法时不会显示)。这是正确的答案!
    【解决方案2】:
    <% 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 %>
    

    【讨论】:

    • 还有另一种方法:将:value 选项传递给f.label 将做同样的事情。例如&lt;%= f.label :contactmethod, 'SMS', :value =&gt; 'sms' %&gt;。这会正确设置标签标签的“for”属性,这使得单击标签会选择相应的单选按钮。在上面的答案中,简单地使用label helper 将导致在使用 FormBuilder 创建单选按钮时“for”属性不正确
    • 我只是想说,作为 Rails 的新手,我找到了这个答案,我一直在寻找答案。这是不断给予的礼物。好吧,直到我记得正确的语法... :)
    • 如果您正在寻找有条件地设置checkedstackoverflow.com/a/4708921/429521,也请检查此项
    • 对于未来的读者,John Douthat 有这个问题的正确答案。这个答案不再正确。
    【解决方案3】:

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

    - form_for(@message) do |f|
      = f.label :email
    

    这还可以确保任何提交的数据都存储在内存中,以防出现任何验证错误等。

    如果您无法在表单对象上调用表单助手方法,例如,如果您正在使用标签助手(radio_button_tag 等),您可以使用以下方法插入名称:

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

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

    【讨论】:

      【解决方案4】:

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

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

      【讨论】:

        【解决方案5】:

        这是我项目中使用radio 按钮及其labels 进行评级的示例

        <div class="rating">
          <%= form.radio_button :star, '1' %>
          <%= form.label :star, '☆', value: '1' %>
        
          <%= form.radio_button :star, '2' %>
          <%= form.label :star, '☆', value: '2' %>
        
          <%= form.radio_button :star, '3' %>
          <%= form.label :star, '☆', value: '3' %>
        
          <%= form.radio_button :star, '4' %>
          <%= form.label :star, '☆', value: '4' %>
        
          <%= form.radio_button :star, '5' %>
          <%= form.label :star, '☆', value: '5' %>
        </div>
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-01-25
          • 2014-08-06
          • 1970-01-01
          • 2018-08-15
          • 2014-04-15
          • 2021-03-27
          相关资源
          最近更新 更多