[英]Insert glyphicon inside rails form_for text_field
I've been trying to find a way to insert a glyphicon
into a text_field
for a rails form_for
. 我一直在尝试找到一种方法来将glyphicon
插入rails form_for
的text_field
中。 Here is how I have the form setup in rails: 这是我在rails中设置表单的方式:
<div class="form-group">
<%= f.text_field :first_name, :class => "form-control input-sm", :name => "first_name", placeholder: "First Name" %>
<i class="glyphicon glyphicon-info-sign" aria-hidden="true"></i>
</div>
So obviously this will treat the f.text_field
and the i
tags as separate elements. 因此很明显,这会将f.text_field
和i
标记视为单独的元素。 So the i
will be placed below the f.text_field(input)
and not in it. 因此, i
将放置在f.text_field(input)
下方,而不是在其中。 The question therefore stands, is there a way in rails to place an i
or span
tag of some sort, inside the f.text_field
? 因此,问题就来了,有没有办法在f.text_field
内部放置某种i
或span
标签? I know that I can position: relative
the i
element and place it appropriately on top of the input
field that is created by the f.text_field
. 我知道我可以position: relative
对于i
元素,并将其适当地放在由f.text_field
创建的input
字段的f.text_field
。
Any answers or suggestions are greatly appreciated. 任何答案或建议,不胜感激。
You can use a block to make this. 您可以使用一个块来做到这一点。 Try something like this 试试这个
<div class="form-group">
<%= f.text_field :first_name, :class => "form-control input-sm", :name => "first_name", placeholder: "First Name" do %>
<i class="glyphicon glyphicon-info-sign" aria-hidden="true"></i>
<% end %>
</div>
This kind of block syntax works for other helpers too like link_to, button_to, etc. 这种块语法也适用于其他助手,例如link_to,button_to等。
EDIT: Looks like I messed up my answer previously. 编辑:看起来我以前弄乱了我的答案。 Hope this helps. 希望这可以帮助。
You can convert the field to a do block like so: 您可以将字段转换为do块,如下所示:
<%= f.text_field :first_name do %>
:name => "first_name",
:placeholder => "First Name",
:class => "form-control input-sm",
:html => "<i class="glyphicon glyphicon-info-sign></i>"
<% end %>
I think the answer to this question provides good detail: 我认为这个问题的答案提供了很多细节:
Rails, insert span tag in form_for label custom text Rails,在form_for标签自定义文本中插入span标签
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.