[英]Rails trying to use html in a f.label tag
I'm trying to achieve this: 我正在努力实现这个目标:
<label ...>First name<em>*</em></label>
<input ... />
with this: 有了这个:
<%= d.label :first_name do %>First Name<em>*</em><% end %>
<%= d.text_field :first_name, :required => "required" %>
but it's actually producing: 但实际上正在产生:
First Name
<em>*</em>
<label ...>
First Name
<em>*</em>
</label>
<input .../>
How can i get the html output that i'm trying to achieve? 我怎样才能获得我想要实现的html输出?
只需将标签内容作为第二个参数传递给块,并使用.html_safe
来阻止它被编码:
<%= d.label :first_name, "First Name<em>*</em>".html_safe
尝试原始
<%= d.label :first_name, raw("First Name<em>*</em>") %>
While both of these answers get the job done, it is not really the "most correct" way from a web design perspective. 尽管这两个答案都能完成工作,但从Web设计的角度来看,这并不是真正的“最正确”方法。
Instead, consider giving the label an html class like "required". 相反,请考虑为标签提供html类,例如“ required”。 Then in your CSS, define a selector for label elements of the "required" class that adds a * and styles it as you wish. 然后在CSS中,为“required”类的标签元素定义一个选择器,该类添加一个*并根据需要对其进行样式设置。
Here's some SCSS from one of my projects: 这是我的一个项目中的一些SCSS:
.required::before {
content: '*';
color: $orange;
margin-right: 2px;
position: relative;
left: -2px;
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.