#layouts/application.html.haml
%title
= yield(:title) || "Default page title"
= " | DefaultSite.com"
#application_helper.rb
def title(page_title)
content_for(:title) { page_title }
end
即使我没有为页面设置title
(辅助方法),"Default page title"
也永远不会显示。而不是它,""
(yield(:title)
的值)出于某种原因出现了。
为什么会这样?
最佳答案
这只是 Rails 设计的一部分。您无法更改此行为。
你应该检查 yield(:title).empty 是否为空?是 true 还是 false 以查看是否调用了 content_for(:title)。
例如,代替:
yield(:title) || "Default page title"
你想要:
(yield(:title).empty? ? "Default page title" : yield(:title))
这只是检查它是否为空,如果为空则应用默认值,否则使用标题。
关于ruby-on-rails-3.2 - 产量(:title) in Ruby on Rails is never equal to false for some reason,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12233319/