我开发了Rails应用程序。Rails:如何不在“<title>”标签中逃脱
&
等字符在<title>
标记中被转义为&
。
例如,在标题中显示foo & bar
,例如foo & bar
。
当我使用raw
时,它可以像我期望的那样显示。但我不想使用raw
,因为数据将由用户输入。
我想显示像this post(请只看标题,内容与我的问题无关)。尽管由用户输入,标题中仍显示&
。
我的代码如下。
application.html.erb
<head>
<title><%= full_title(yield(:title)) %></title>
...
application_helper.rb
def full_title(page_title = '')
base_title = "app name"
if page_title.empty?
base_title
else
page_title + " | " + base_title
end
end
我在一些视图文件添加provide
,如show.html.erb。
<% provide(:title, @schedule.title) %> #this title is inputted by user
是否可以逃脱它,但允许一些字符未转义?
使用'.html_safe'方法 – uzaif
.html_safe等价于使用raw。切勿在用户输入时使用它。 – hypern
我很困惑。那么你想逃避还是不? –