[英]rails not updating page title
我正在关注railscasts以更新自定义页面标题,并意识到它不再起作用。 因此,我根据评论更新了代码如下。 如果没有设置标题,我会看到“我的服务-”,而我希望它包含默认的标题值集。 有什么见解吗?
在application.html.erb
:
<!DOCTYPE html>
<html>
<%= render 'layouts/head' %>
<!-- <body> included in yield -->
<%= yield %>
<!-- </body> -->
</html>
在_head.html.erb
<head>
<title>My services - <%= yield(:title) %> </title>
</head>
在home.html.erb
[有意不设置标题以查看默认值]
<body></body>
在application_helper.rb
def title(page_title, default="Testing")
content_for(:title) { page_title || default }
end
在application_helper.rb
,我还尝试了以下解决方案:
def title(page_title)
content_for(:title) { page_title || default }
end
def yield_for(section, default = "Testing")
content_for?(section) ? yield(section) : default
end
有什么见解吗?
我认为您应该简化:
<title>My services - <%= page_title %> </title>
application_helper.rb
def page_title
if content_for?(:title)
content_for(:title)
else
"Testing"
end
end
现在,我认为您实际上并不需要“测试” ...确实,我认为您只是不想在html页面标题的末尾看到“-”。 那么为什么不呢?
<title><%= html_title %></title>
def html_title
site_name = "My services"
page_title = content_for(:title) if content_for?(:title)
[site_name,page_title].join(" - ")
end
您将看到:
<title>My services</title>
或者,如果您这样设置标题:
<%= content_for(:title) { "SuperHero" } %>
你会看到的:
<title>My services - SuperHero</title>
#content_for? 定义为:
#content_for? simply checks whether any content has been captured yet using #content_for Useful to render parts of your layout differently based on what is in your views.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.