我想使用f.label方法创建我的表单元素标签,但是 – 我想要将表单元素嵌套在标签内。这可能吗?
– 从W3C –
To associate a label with another control implicitly,the control element must be within the contents of the LABEL element. In this case,the LABEL may only contain one control element. The label itself may be positioned before or after the associated control.
In this example,we implicitly associate two labels with two text input controls:
<form action="..." method="post"> <p> <label> First Name <input type="text" name="firstname" /> </label> <label> <input type="text" name="lastname" /> Last Name </label> </p> </form>
解决方法
您可以使用块将表单帮助程序嵌套在标签内。以下是使用HAML的示例,但它也适用于ERB。
= form_for your_resource do |f| = f.label :first_name do = f.text_field :first_name