将块传递到rails3中的标签助手 [英] Passing block to label helper in rails3

查看:82
本文介绍了将块传递到rails3中的标签助手的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建带有一些嵌套元素的标签标签.我正在使用标签辅助程序,并尝试将内部html作为块传递,但生成的HTML看起来不符合我的预期. ERB:

I want to create label tag with some nested elements. I am using label helper and trying to pass inner html as block but generated HTML doesn't look as I expected. ERB:

<span>Span element</span>
<%= label("object", "method") do %>
  <span>Inner span</span>
<% end %>

HTML输出:

<span>Span element</span> 
<span>Inner span</span> 

<label for="object_method">
<span>Span element</span> 
  <span>Inner span</span> 
</label>

当我使用<%%>标记传递内部html时,其输出应为:
ERB:

When I pass inner html using <% %> markups output is as it should be:
ERB:

<span>Span element</span>
<%= label("object", "method") do %>
  <% raw '<span>Inner span</span>' %>
<% end %>

HTML输出:

<span>Span element</span>
<label for="object_method">
  <span>Inner span</span>
</label>

我想知道这是我在ActionView标签助手中的错误还是错误.对于其他助手,阻止传球效果很好.

I am wondering if it is my mistake or bug in ActionView label helper. For other helpers block passing works fine.

谢谢, Michał

推荐答案

我的理解是,在这种情况下,您需要使用label_tag帮助器:

My understanding is that you need to use the label_tag helper in this case:

<%= label_tag "my_label_name" do %>
  <span>Inner span</span>
<% end %>

这样做的原因是,尽管表单标签帮助器(使用模型对象属性)为您填写了"for"属性,但您不需要嵌套元素.

The reason for this is that although the form label helper fills out the "for" attribute for you (using your model object attribute), you don't need it with nested elements.

当您有一个打开的标签标签(而不是自动关闭的标签)包装内部内容时,不需要"for"属性,因为标签显然与其嵌套内容相关联(这称为隐式关联) ).

When you have an open label tag (rather than self-closing), that wraps the inner content, the "for" attribute is not needed, because the label is obviously associated with its nested content (this is known as implicit association).

所以,这是预期的行为-看起来Rails团队是故意以此方式构建的.

So, this is expected behaviour - it looks like the Rails team have deliberately built it this way.

这篇关于将块传递到rails3中的标签助手的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆